System.Type.MakeByRefType 方法

方法描述

返回表示作为 ref 参数(在 Visual Basic 中为 ByRef 参数)传递时的当前类型的 Type 对象。

语法定义(C# System.Type.MakeByRefType 方法 的用法)

public virtual Type MakeByRefType()

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
返回值 System.Type 表示作为 ref 参数(在 Visual Basic 中为 ByRef 参数)传递时的当前类型的 Type 对象。

提示和注释

MakeByRefType 方法提供了一种为参数列表生成 ref 类型(在 Visual Basic 中为 ByRef)的方法。

使用 Microsoft 中间语言 (MSIL) 的语法,如果当前 Type 对象表示 Int32,则此方法将返回表示 Int32& 的 Type 对象。

System.Type.MakeByRefType 方法例子

下面的代码示例为 Test 类创建数组、ref(在 Visual Basic 中为 ByRef)和指针类型。

using System;
using System.Reflection;

public class Example
{
    public static void Main()
    {
        // Create a Type object that represents a one-dimensional
        // array of Example objects.
        Type t = typeof(Example).MakeArrayType();
        Console.WriteLine("\r\nArray of Example: {0}", t);

        // Create a Type object that represents a two-dimensional
        // array of Example objects.
        t = typeof(Example).MakeArrayType(2);
        Console.WriteLine("\r\nTwo-dimensional array of Example: {0}", t);

        // Demonstrate an exception when an invalid array rank is
        // specified.
        try
        {
            t = typeof(Example).MakeArrayType(-1);
        }
        catch(Exception ex)
        {
            Console.WriteLine("\r\n{0}", ex);
        }

        // Create a Type object that represents a ByRef parameter
        // of type Example.
        t = typeof(Example).MakeByRefType();
        Console.WriteLine("\r\nByRef Example: {0}", t);

        // Get a Type object representing the Example class, a
        // MethodInfo representing the "Test" method, a ParameterInfo
        // representing the parameter of type Example, and finally
        // a Type object representing the type of this ByRef parameter.
        // Compare this Type object with the Type object created using
        // MakeByRefType.
        Type t2 = typeof(Example);
        MethodInfo mi = t2.GetMethod("Test");
        ParameterInfo pi = mi.GetParameters()[0];
        Type pt = pi.ParameterType;
        Console.WriteLine("Are the ByRef types equal? {0}", (t == pt));

        // Create a Type object that represents a pointer to an
        // Example object.
        t = typeof(Example).MakePointerType();
        Console.WriteLine("\r\nPointer to Example: {0}", t);
    }

    // A sample method with a ByRef parameter.
    //
    public void Test(ref Example e)
    {
    }
}

/* This example produces output similar to the following:

Array of Example: Example[]

Two-dimensional array of Example: Example[,]

System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at System.RuntimeType.MakeArrayType(Int32 rank) in c:\vbl\ndp\clr\src\BCL\System\RtType.cs:line 2999
   at Example.Main()

ByRef Example: Example&
Are the ByRef types equal? True

Pointer to Example: Example*

 */

异常

异常 异常描述
NotSupportedException 基类不支持所调用的方法。
TypeLoadException
  • 当前类型为 TypedReference。
  • 当前类型是 ByRef 类型。 即,Type.IsByRef 返回 true。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。