Type.HasElementType Proprietà
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
public:
property bool HasElementType { bool get(); };
public bool HasElementType { get; }
member this.HasElementType : bool
Public ReadOnly Property HasElementType As Boolean
true
se Type è una matrice, un puntatore oppure è passato per riferimento; in caso contrario, false
.
L'esempio seguente restituisce true
o false
a seconda che l'oggetto sia una matrice, un tipo di riferimento o un puntatore.
using namespace System;
using namespace System::Reflection;
using namespace System::Runtime::InteropServices;
public ref class Example
{
public:
// This method is for demonstration purposes. It includes a
// tracking reference (C# ref, VB ByRef), an out parameter,
// and a pointer.
void Test(int% x, [OutAttribute()] int% y, int* z)
{
*z = x = y = 0;
}
};
int main()
{
// All of the following display 'True'.
// Define a managed array, get its type, and display HasElementType.
array<Example^>^ examples = {gcnew Example(), gcnew Example()};
Type^ t = examples::typeid;
Console::WriteLine(t);
Console::WriteLine("HasElementType is '{0}' for managed array types.", t->HasElementType);
// When you use Reflection Emit to emit dynamic methods and
// assemblies, you can create array types using MakeArrayType.
// The following creates the type 'array of Example'.
t = Example::typeid->MakeArrayType();
Console::WriteLine("HasElementType is '{0}' for managed array types.", t->HasElementType);
// When you reflect over methods, HasElementType is true for
// ref, out, and pointer parameter types. The following
// gets the Test method, defined above, and examines its
// parameters.
MethodInfo^ mi = Example::typeid->GetMethod("Test");
array<ParameterInfo^>^ parms = mi->GetParameters();
t = parms[0]->ParameterType;
Console::WriteLine("HasElementType is '{0}' for ref parameter types.", t->HasElementType);
t = parms[1]->ParameterType;
Console::WriteLine("HasElementType is '{0}' for out parameter types.", t->HasElementType);
t = parms[2]->ParameterType;
Console::WriteLine("HasElementType is '{0}' for pointer parameter types.", t->HasElementType);
// When you use Reflection Emit to emit dynamic methods and
// assemblies, you can create pointer and ByRef types to use
// when you define method parameters.
t = Example::typeid->MakePointerType();
Console::WriteLine("HasElementType is '{0}' for pointer types.", t->HasElementType);
t = Example::typeid->MakeByRefType();
Console::WriteLine("HasElementType is '{0}' for ByRef types.", t->HasElementType);
}
// This code must be compiled with the /unsafe switch:
// csc /unsafe source.cs
using System;
using System.Reflection;
public class Example
{
// This method is for demonstration purposes.
unsafe public void Test(ref int x, out int y, int* z)
{
*z = x = y = 0;
}
public static void Main()
{
// All of the following display 'True'.
// Define an array, get its type, and display HasElementType.
int[] nums = {1, 1, 2, 3, 5, 8, 13};
Type t = nums.GetType();
Console.WriteLine("HasElementType is '{0}' for array types.", t.HasElementType);
// Test an array type without defining an array.
t = typeof(Example[]);
Console.WriteLine("HasElementType is '{0}' for array types.", t.HasElementType);
// When you use Reflection Emit to emit dynamic methods and
// assemblies, you can create array types using MakeArrayType.
// The following creates the type 'array of Example'.
t = typeof(Example).MakeArrayType();
Console.WriteLine("HasElementType is '{0}' for array types.", t.HasElementType);
// When you reflect over methods, HasElementType is true for
// ref, out, and pointer parameter types. The following
// gets the Test method, defined above, and examines its
// parameters.
MethodInfo mi = typeof(Example).GetMethod("Test");
ParameterInfo[] parms = mi.GetParameters();
t = parms[0].ParameterType;
Console.WriteLine("HasElementType is '{0}' for ref parameter types.", t.HasElementType);
t = parms[1].ParameterType;
Console.WriteLine("HasElementType is '{0}' for out parameter types.", t.HasElementType);
t = parms[2].ParameterType;
Console.WriteLine("HasElementType is '{0}' for pointer parameter types.", t.HasElementType);
// When you use Reflection Emit to emit dynamic methods and
// assemblies, you can create pointer and ByRef types to use
// when you define method parameters.
t = typeof(Example).MakePointerType();
Console.WriteLine("HasElementType is '{0}' for pointer types.", t.HasElementType);
t = typeof(Example).MakeByRefType();
Console.WriteLine("HasElementType is '{0}' for ByRef types.", t.HasElementType);
}
}
#nowarn "9"
open FSharp.NativeInterop
type Example() =
// This method is for demonstration purposes.
member _.Test(x: int byref, y: int outref, z: int nativeptr) =
x <- 0
y <- 0
NativePtr.write z 0
// All of the following display 'True'.
do
// Define an array, get its type, and display HasElementType.
let nums = [| 1; 1; 2; 3; 5; 8; 13 |]
let t = nums.GetType()
printfn $"HasElementType is '{t.HasElementType}' for array types."
// Test an array type without defining an array.
let t = typeof<Example[]>
printfn $"HasElementType is '{t.HasElementType}' for array types."
// When you use Reflection Emit to emit dynamic methods and
// assemblies, you can create array types using MakeArrayType.
// The following creates the type 'array of Example'.
let t = typeof<Example>.MakeArrayType()
printfn $"HasElementType is '{t.HasElementType}' for array types."
// When you reflect over methods, HasElementType is true for
// byref, outref, and pointer parameter types. The following
// gets the Test method, defined above, and examines its
// parameters.
let mi = typeof<Example>.GetMethod "Test"
let parms = mi.GetParameters()
let t = parms[0].ParameterType
printfn $"HasElementType is '{t.HasElementType}' for ref parameter types."
let t = parms[1].ParameterType
printfn $"HasElementType is '{t.HasElementType}' for out parameter types."
let t = parms[2].ParameterType
printfn $"HasElementType is '{t.HasElementType}' for pointer parameter types."
// When you use Reflection Emit to emit dynamic methods and
// assemblies, you can create pointer and ByRef types to use
// when you define method parameters.
let t = typeof<Example>.MakePointerType()
printfn $"HasElementType is '{t.HasElementType}' for pointer types."
let t = typeof<Example>.MakeByRefType()
printfn $"HasElementType is '{t.HasElementType}' for ByRef types."
Imports System.Reflection
Imports System.Runtime.InteropServices
Public Class Example
' This method is for demonstration purposes.
Public Shared Sub Test(ByRef x As Integer, <Out> ByRef y As Integer)
End Sub
Public Shared Sub Main()
' All of the following display 'True'.
' Define an array, get its type, and display HasElementType.
Dim nums() As Integer = {1, 1, 2, 3, 5, 8, 13}
Dim t As Type = nums.GetType()
Console.WriteLine("HasElementType is '{0}' for array types.", t.HasElementType)
' Test an array type without defining an array.
t = GetType(Example())
Console.WriteLine("HasElementType is '{0}' for array types.", t.HasElementType)
' When you use Reflection Emit to emit dynamic methods and
' assemblies, you can create array types using MakeArrayType.
' The following creates the type 'array of Example'.
t = GetType(Example).MakeArrayType()
Console.WriteLine("HasElementType is '{0}' for array types.", t.HasElementType)
' When you reflect over methods, HasElementType is true for
' ref, out, and pointer parameter types. The following
' gets the Test method, defined above, and examines its
' parameters.
Dim mi As MethodInfo = GetType(Example).GetMethod("Test")
Dim parms() As ParameterInfo = mi.GetParameters()
t = parms(0).ParameterType
Console.WriteLine("HasElementType is '{0}' for ref parameter types.", t.HasElementType)
t = parms(1).ParameterType
Console.WriteLine("HasElementType is '{0}' for <Out> parameter types.", t.HasElementType)
' When you use Reflection Emit to emit dynamic methods and
' assemblies, you can create pointer and ByRef types to use
' when you define method parameters.
t = GetType(Example).MakePointerType()
Console.WriteLine("HasElementType is '{0}' for pointer types.", t.HasElementType)
t = GetType(Example).MakeByRefType()
Console.WriteLine("HasElementType is '{0}' for ByRef types.", t.HasElementType)
End Sub
End Class
Ad esempio, Type.GetType("Int32[]"). HasElementType restituisce true
, ma Type.GetType("Int32"). HasElementType restituisce false
. HasElementType restituisce true
anche per "Int32*" e "Int32&".
Se l'oggetto corrente Type rappresenta un tipo generico o un parametro di tipo nella definizione di un tipo generico o di un metodo generico, questa proprietà restituisce false
sempre .
Prodotto | Versioni |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
Feedback su .NET
.NET è un progetto di open source. Selezionare un collegamento per fornire feedback: