Type.IsByRef 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取一个值,该值指示 Type 是否由引用传递。
public:
virtual property bool IsByRef { bool get(); };
public:
property bool IsByRef { bool get(); };
public virtual bool IsByRef { get; }
public bool IsByRef { get; }
member this.IsByRef : bool
Public Overridable ReadOnly Property IsByRef As Boolean
Public ReadOnly Property IsByRef As Boolean
属性值
如果 Type 按引用传递,则为 true
;否则为 false
。
实现
示例
下面的示例演示如何使用 属性检查是否通过引用 IsByRef
传递指定类型。 该示例定义类 MyTypeDelegator
,该类替代 HasElementTypeImpl
方法。 主类检查 属性 HasElementType
并显示元素类型。
using namespace System;
using namespace System::Reflection;
public ref class MyTypeDelegator: public TypeDelegator
{
public:
String^ myElementType;
private:
Type^ myType;
public:
MyTypeDelegator( Type^ myType )
: TypeDelegator( myType )
{
this->myType = myType;
}
protected:
// Override Type::HasElementTypeImpl().
virtual bool HasElementTypeImpl() override
{
// Determine whether the type is an array.
if ( myType->IsArray )
{
myElementType = "array";
return true;
}
// Determine whether the type is a reference.
if ( myType->IsByRef )
{
myElementType = "reference";
return true;
}
// Determine whether the type is a pointer.
if ( myType->IsPointer )
{
myElementType = "pointer";
return true;
}
// Return false if the type is not a reference, array, or pointer type.
return false;
}
};
int main()
{
try
{
int myInt = 0;
array<Int32>^myArray = gcnew array<Int32>(5);
MyTypeDelegator^ myType = gcnew MyTypeDelegator( myArray->GetType() );
// Determine whether myType is an array, pointer, reference type.
Console::WriteLine( "\nDetermine whether a variable is an array, pointer, or reference type.\n" );
if ( myType->HasElementType )
Console::WriteLine( "The type of myArray is {0}.", myType->myElementType );
else
Console::WriteLine( "myArray is not an array, pointer, or reference type." );
myType = gcnew MyTypeDelegator( myInt.GetType() );
// Determine whether myType is an array, pointer, reference type.
if ( myType->HasElementType )
Console::WriteLine( "The type of myInt is {0}.", myType->myElementType );
else
Console::WriteLine( "myInt is not an array, pointer, or reference type." );
}
catch ( Exception^ e )
{
Console::WriteLine( "Exception: {0}", e->Message );
}
}
using System;
using System.Reflection;
public class MyTypeDelegator : TypeDelegator
{
public string myElementType = null;
private Type myType = null ;
public MyTypeDelegator(Type myType) : base(myType)
{
this.myType = myType;
}
// Override Type.HasElementTypeImpl().
protected override bool HasElementTypeImpl()
{
// Determine whether the type is an array.
if(myType.IsArray)
{
myElementType = "array";
return true;
}
// Determine whether the type is a reference.
if(myType.IsByRef)
{
myElementType = "reference";
return true;
}
// Determine whether the type is a pointer.
if(myType.IsPointer)
{
myElementType = "pointer";
return true;
}
// Return false if the type is not a reference, array, or pointer type.
return false;
}
}
public class Type_HasElementTypeImpl
{
public static void Main()
{
try
{
int myInt = 0 ;
int[] myArray = new int[5];
MyTypeDelegator myType = new MyTypeDelegator(myArray.GetType());
// Determine whether myType is an array, pointer, reference type.
Console.WriteLine("\nDetermine whether a variable is an array, pointer, or reference type.\n");
if( myType.HasElementType)
Console.WriteLine("The type of myArray is {0}.", myType.myElementType);
else
Console.WriteLine("myArray is not an array, pointer, or reference type.");
myType = new MyTypeDelegator(myInt.GetType());
// Determine whether myType is an array, pointer, reference type.
if( myType.HasElementType)
Console.WriteLine("The type of myInt is {0}.", myType.myElementType);
else
Console.WriteLine("myInt is not an array, pointer, or reference type.");
}
catch( Exception e )
{
Console.WriteLine("Exception: {0}", e.Message);
}
}
}
Imports System.Reflection
Public Class MyTypeDelegator
Inherits TypeDelegator
Public myElementType As String = Nothing
Private myType As Type = Nothing
Public Sub New(ByVal myType As Type)
MyBase.New(myType)
Me.myType = myType
End Sub
' Override Type.HasElementTypeImpl().
Protected Overrides Function HasElementTypeImpl() As Boolean
' Determine whether the type is an array.
If myType.IsArray Then
myElementType = "array"
Return True
End If
' Determine whether the type is a reference.
If myType.IsByRef Then
myElementType = "reference"
Return True
End If
' Determine whether the type is a pointer.
If myType.IsPointer Then
myElementType = "pointer"
Return True
End If
' The type is not a reference, array, or pointer type.
Return False
End Function 'HasElementTypeImpl
End Class
Public Class Type_HasElementTypeImpl
Public Shared Sub Main()
Try
Dim myInt As Integer = 0
Dim myArray(4) As Integer
Dim myType As New MyTypeDelegator(myArray.GetType())
Console.WriteLine(ControlChars.NewLine + "Determine whether a variable refers to an array or pointer or reference type." + ControlChars.NewLine)
' Determine whether myType is an array, pointer, reference type.
If myType.HasElementType Then
Console.WriteLine("The type of myArray is {0}.", myType.myElementType.ToString())
Else
Console.WriteLine("myArray is not an array, pointer, or reference type.")
End If
myType = New MyTypeDelegator(myInt.GetType())
' Determine whether myType is an array, pointer, reference type.
If myType.HasElementType Then
Console.WriteLine("The type of myInt is {0}.", myType.myElementType.ToString())
Else
Console.WriteLine("myInt is not an array, pointer, or reference type.")
End If
Catch e As Exception
Console.WriteLine("Exception: {0}", e.Message.ToString())
End Try
End Sub
End Class
注解
若要获取实际类型,请取消引用通过引用传递的类型,然后对类型 GetElementType 调用 。