Visualizzazione delle informazioni sul tipo
La classe System.Type è fondamentale per la reflection. Quando la reflection lo richiede, Common Language Runtime crea l'oggetto Type relativo a un tipo caricato. Per ottenere informazioni sul tipo, è possibile utilizzare metodi, campi, proprietà e classi annidate dell'oggetto Type.
Utilizzare Assembly.GetType o Assembly.GetTypes per ottenere oggetti Type da assembly che non sono stati caricati, passando il nome del tipo o dei tipi desiderati. Utilizzare Type.GetType per ottenere oggetti Type da un assembly già caricato. Utilizzare Module.GetType e Module.GetTypes per ottenere il modulo degli oggetti Type.
Nota |
---|
Se si desidera esaminare e modificare tipi e metodi generici, vedere le informazioni aggiuntive disponibili in Reflection e tipi generici e Procedura: esaminare e creare istanze di tipi generici tramite reflection. |
Nell'esempio riportato di seguito viene illustrata la sintassi necessaria per ottenere il modulo e l'oggetto Assembly relativi a un assembly.
' Gets the mscorlib assembly in which the object is defined.
Dim a As Assembly = GetType(Object).Module.Assembly
// Gets the mscorlib assembly in which the object is defined.
Assembly a = typeof(object).Module.Assembly;
// Gets the mscorlib assembly in which the object is defined.
Assembly^ a = Object::typeid->Module->Assembly;
Nell'esempio che segue viene illustrato come ottenere oggetti Type da un assembly caricato.
' Loads an assembly using its file name.
Dim a As Assembly = Assembly.LoadFrom("MyExe.exe")
' Gets the type names from the assembly.
Dim types2() As Type = a.GetTypes()
For Each t As Type In types2
Console.WriteLine(t.FullName)
Next t
// Loads an assembly using its file name.
Assembly a = Assembly.LoadFrom("MyExe.exe");
// Gets the type names from the assembly.
Type[] types2 = a.GetTypes();
foreach (Type t in types2)
{
Console.WriteLine(t.FullName);
}
// Loads an assembly using its file name.
Assembly^ a = Assembly::LoadFrom("MyExe.exe");
// Gets the type names from the assembly.
array<Type^>^ types2 = a->GetTypes();
for each (Type^ t in types2)
{
Console::WriteLine(t->FullName);
}
Una volta ottenuto un oggetto Type, è possibile avvalersi di svariati modi per ottenere informazioni sui membri del tipo. Per ottenere informazioni su tutti i membri del tipo è ad esempio possibile chiamare il metodo Type.GetMembers, che ottiene una matrice di oggetti MemberInfo, ciascuno dei quali descrive un membro del tipo.
È inoltre possibile utilizzare i metodi della classe Type per recuperare informazioni su uno o più costruttori, metodi, eventi, campi o proprietà di cui si specifica il nome. Type.GetConstructor, ad esempio, incapsula uno specifico costruttore della classe corrente.
Se si dispone di un oggetto Type, sarà possibile utilizzare la proprietà Type.Module per ottenere un oggetto che incapsula il modulo contenente il tipo. Per individuare un oggetto che incapsula l'assembly contenente il modulo, utilizzare la proprietà Module.Assembly. Per ottenere direttamente l'assembly che incapsula il tipo, utilizzare la proprietà Type.Assembly.
System.Type e ConstructorInfo
Nell'esempio riportato di seguito viene illustrato come elencare i costruttori di una classe, nel caso specifico la classe String.
' This program lists all the public constructors
' of the System.String class.
Imports System
Imports System.Reflection
Class ListMembers
Public Shared Sub Main()
Dim t As Type = GetType(String)
Console.WriteLine("Listing all the public constructors of the {0} type", t)
' Constructors.
Dim ci As ConstructorInfo() = t.GetConstructors((BindingFlags.Public Or BindingFlags.Instance))
Console.WriteLine("//Constructors")
PrintMembers(ci)
End Sub
Public Shared Sub PrintMembers(ms() As MemberInfo)
Dim m As MemberInfo
For Each m In ms
Console.WriteLine("{0}{1}", " ", m)
Next m
Console.WriteLine()
End Sub
End Class
// This program lists all the public constructors
// of the System.String class.
using System;
using System.Reflection;
class ListMembers
{
public static void Main()
{
Type t = typeof(System.String);
Console.WriteLine("Listing all the public constructors of the {0} type", t);
// Constructors.
ConstructorInfo[] ci = t.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
Console.WriteLine("//Constructors");
PrintMembers(ci);
}
public static void PrintMembers(MemberInfo[] ms)
{
foreach (MemberInfo m in ms)
{
Console.WriteLine("{0}{1}", " ", m);
}
Console.WriteLine();
}
}
// This program lists all the public constructors
// of the System.String class.
using namespace System;
using namespace System::Reflection;
class ListMembers
{
public:
static void Main()
{
Type^ t = System::String::typeid;
Console::WriteLine ("Listing all the public constructors of the {0} type", t);
// Constructors.
array<ConstructorInfo^>^ ci = t->GetConstructors(BindingFlags::Public | BindingFlags::Instance);
Console::WriteLine ("//Constructors");
PrintMembers(ci);
}
static void PrintMembers(array<MemberInfo^>^ ms)
{
for each (MemberInfo^ m in ms)
{
Console::WriteLine ("{0}{1}", " ", m);
}
Console::WriteLine();
}
};
int main()
{
ListMembers::Main();
}
MemberInfo, MethodInfo, FieldInfo e PropertyInfo
Per ottenere informazioni su metodi, proprietà, eventi e campi del tipo, utilizzare gli oggetti MemberInfo, MethodInfo, FieldInfo o PropertyInfo.
Nell'esempio riportato di seguito viene utilizzato MemberInfo per elencare il numero di membri della classe System.IO.File e viene utilizzata la proprietà System.Type.IsPublic per determinare la visibilità della classe.
Imports System
Imports System.IO
Imports System.Reflection
Class Mymemberinfo
Public Shared Sub Main()
Console.WriteLine ("\nReflection.MemberInfo")
' Gets the Type and MemberInfo.
Dim MyType As Type = Type.GetType("System.IO.File")
Dim Mymemberinfoarray() As MemberInfo = MyType.GetMembers()
' Gets and displays the DeclaringType method.
Console.WriteLine("\nThere are {0} members in {1}.",
Mymemberinfoarray.Length, MyType.FullName)
Console.WriteLine("{0}.", MyType.FullName)
If MyType.IsPublic
Console.WriteLine("{0} is public.", MyType.FullName)
End If
End Sub
End Class
using System;
using System.IO;
using System.Reflection;
class Mymemberinfo
{
public static void Main()
{
Console.WriteLine ("\nReflection.MemberInfo");
// Gets the Type and MemberInfo.
Type MyType = Type.GetType("System.IO.File");
MemberInfo[] Mymemberinfoarray = MyType.GetMembers();
// Gets and displays the DeclaringType method.
Console.WriteLine("\nThere are {0} members in {1}.",
Mymemberinfoarray.Length, MyType.FullName);
Console.WriteLine("{0}.", MyType.FullName);
if (MyType.IsPublic)
{
Console.WriteLine("{0} is public.", MyType.FullName);
}
}
}
using namespace System;
using namespace System::IO;
using namespace System::Reflection;
public ref class Mymemberinfo
{
public:
static void Main()
{
Console::WriteLine ("\nReflection.MemberInfo");
// Gets the Type and MemberInfo.
Type^ MyType = Type::GetType("System.IO.File");
array<MemberInfo^>^ Mymemberinfoarray = MyType->GetMembers();
// Gets and displays the DeclaringType method.
Console::WriteLine("\nThere are {0} members in {1}.",
Mymemberinfoarray->Length, MyType->FullName);
Console::WriteLine("{0}.", MyType->FullName);
if (MyType->IsPublic)
{
Console::WriteLine("{0} is public.", MyType->FullName);
}
}
};
int main()
{
Mymemberinfo::Main();
}
Nell'esempio che segue viene analizzato il tipo del membro specificato. Viene eseguita una reflection su un membro della classe MemberInfo e ne viene elencato il tipo.
' This code displays information about the GetValue method of FieldInfo.
Imports System
Imports System.Reflection
Class MyMethodInfo
Public Shared Sub Main()
Console.WriteLine("Reflection.MethodInfo")
' Gets and displays the Type.
Dim MyType As Type = Type.GetType("System.Reflection.FieldInfo")
' Specifies the member for which you want type information.
Dim Mymethodinfo As MethodInfo = MyType.GetMethod("GetValue")
Console.WriteLine((MyType.FullName & "." & Mymethodinfo.Name))
' Gets and displays the MemberType property.
Dim Mymembertypes As MemberTypes = Mymethodinfo.MemberType
If MemberTypes.Constructor = Mymembertypes Then
Console.WriteLine("MemberType is of type All")
ElseIf MemberTypes.Custom = Mymembertypes Then
Console.WriteLine("MemberType is of type Custom")
ElseIf MemberTypes.Event = Mymembertypes Then
Console.WriteLine("MemberType is of type Event")
ElseIf MemberTypes.Field = Mymembertypes Then
Console.WriteLine("MemberType is of type Field")
ElseIf MemberTypes.Method = Mymembertypes Then
Console.WriteLine("MemberType is of type Method")
ElseIf MemberTypes.Property = Mymembertypes Then
Console.WriteLine("MemberType is of type Property")
ElseIf MemberTypes.TypeInfo = Mymembertypes Then
Console.WriteLine("MemberType is of type TypeInfo")
End If
Return
End Sub
End Class
// This code displays information about the GetValue method of FieldInfo.
using System;
using System.Reflection;
class MyMethodInfo
{
public static int Main()
{
Console.WriteLine("Reflection.MethodInfo");
// Gets and displays the Type.
Type MyType = Type.GetType("System.Reflection.FieldInfo");
// Specifies the member for which you want type information.
MethodInfo Mymethodinfo = MyType.GetMethod("GetValue");
Console.WriteLine(MyType.FullName + "." + Mymethodinfo.Name);
// Gets and displays the MemberType property.
MemberTypes Mymembertypes = Mymethodinfo.MemberType;
if (MemberTypes.Constructor == Mymembertypes)
{
Console.WriteLine("MemberType is of type All");
}
else if (MemberTypes.Custom == Mymembertypes)
{
Console.WriteLine("MemberType is of type Custom");
}
else if (MemberTypes.Event == Mymembertypes)
{
Console.WriteLine("MemberType is of type Event");
}
else if (MemberTypes.Field == Mymembertypes)
{
Console.WriteLine("MemberType is of type Field");
}
else if (MemberTypes.Method == Mymembertypes)
{
Console.WriteLine("MemberType is of type Method");
}
else if (MemberTypes.Property == Mymembertypes)
{
Console.WriteLine("MemberType is of type Property");
}
else if (MemberTypes.TypeInfo == Mymembertypes)
{
Console.WriteLine("MemberType is of type TypeInfo");
}
return 0;
}
}
// This code displays information about the GetValue method of FieldInfo.
using namespace System;
using namespace System::Reflection;
public ref class MyMethodInfo
{
public:
static int Main()
{
Console::WriteLine("Reflection.MethodInfo");
// Gets and displays the Type.
Type^ MyType = Type::GetType("System.Reflection.FieldInfo");
// Specifies the member for which you want type information.
MethodInfo^ Mymethodinfo = MyType->GetMethod("GetValue");
Console::WriteLine(MyType->FullName + "." + Mymethodinfo->Name);
// Gets and displays the MemberType property.
MemberTypes Mymembertypes = Mymethodinfo->MemberType;
if (MemberTypes::Constructor == Mymembertypes)
{
Console::WriteLine("MemberType is of type All");
}
else if (MemberTypes::Custom == Mymembertypes)
{
Console::WriteLine("MemberType is of type Custom");
}
else if (MemberTypes::Event == Mymembertypes)
{
Console::WriteLine("MemberType is of type Event");
}
else if (MemberTypes::Field == Mymembertypes)
{
Console::WriteLine("MemberType is of type Field");
}
else if (MemberTypes::Method == Mymembertypes)
{
Console::WriteLine("MemberType is of type Method");
}
else if (MemberTypes::Property == Mymembertypes)
{
Console::WriteLine("MemberType is of type Property");
}
else if (MemberTypes::TypeInfo == Mymembertypes)
{
Console::WriteLine("MemberType is of type TypeInfo");
}
return 0;
}
};
int main()
{
MyMethodInfo::Main();
}
Nell'esempio riportato di seguito vengono utilizzate tutte le classi *Info di Reflection insieme a BindingFlags per elencare tutti i membri (costruttori, campi, proprietà, eventi e metodi) della classe specificata, distinguendo tra membri statici e di istanza.
' This program lists all the members of the
' System.IO.BufferedStream class.
Imports System
Imports System.IO
Imports System.Reflection
Class ListMembers
Public Shared Sub Main()
' Specifies the class.
Dim t As Type = GetType(System.IO.BufferedStream)
Console.WriteLine("Listing all the members (public and non public) of the {0} type", t)
' Lists static fields first.
Dim fi As FieldInfo() = t.GetFields((BindingFlags.Static Or BindingFlags.NonPublic Or BindingFlags.Public))
Console.WriteLine("// Static Fields")
PrintMembers(fi)
' Static properties.
Dim pi As PropertyInfo() = t.GetProperties((BindingFlags.Static Or BindingFlags.NonPublic Or BindingFlags.Public))
Console.WriteLine("// Static Properties")
PrintMembers(pi)
' Static events.
Dim ei As EventInfo() = t.GetEvents((BindingFlags.Static Or BindingFlags.NonPublic Or BindingFlags.Public))
Console.WriteLine("// Static Events")
PrintMembers(ei)
' Static methods.
Dim mi As MethodInfo() = t.GetMethods((BindingFlags.Static Or BindingFlags.NonPublic Or BindingFlags.Public))
Console.WriteLine("// Static Methods")
PrintMembers(mi)
' Constructors.
Dim ci As ConstructorInfo() = t.GetConstructors((BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public))
Console.WriteLine("// Constructors")
PrintMembers(ci)
' Instance fields.
fi = t.GetFields((BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public))
Console.WriteLine("// Instance Fields")
PrintMembers(fi)
' Instance properites.
pi = t.GetProperties((BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public))
Console.WriteLine("// Instance Properties")
PrintMembers(pi)
' Instance events.
ei = t.GetEvents((BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public))
Console.WriteLine("// Instance Events")
PrintMembers(ei)
' Instance methods.
mi = t.GetMethods((BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public))
Console.WriteLine("// Instance Methods")
PrintMembers(mi)
Console.WriteLine(ControlChars.CrLf & "Press ENTER to exit.")
Console.Read()
End Sub
Public Shared Sub PrintMembers(ms() As MemberInfo)
Dim m As MemberInfo
For Each m In ms
Console.WriteLine("{0}{1}", " ", m)
Next m
Console.WriteLine()
End Sub
End Class
// This program lists all the members of the
// System.IO.BufferedStream class.
using System;
using System.IO;
using System.Reflection;
class ListMembers
{
public static void Main()
{
// Specifies the class.
Type t = typeof(System.IO.BufferedStream);
Console.WriteLine("Listing all the members (public and non public) of the {0} type", t);
// Lists static fields first.
FieldInfo[] fi = t.GetFields(BindingFlags.Static |
BindingFlags.NonPublic | BindingFlags.Public);
Console.WriteLine("// Static Fields");
PrintMembers(fi);
// Static properties.
PropertyInfo[] pi = t.GetProperties(BindingFlags.Static |
BindingFlags.NonPublic | BindingFlags.Public);
Console.WriteLine("// Static Properties");
PrintMembers(pi);
// Static events.
EventInfo[] ei = t.GetEvents(BindingFlags.Static |
BindingFlags.NonPublic | BindingFlags.Public);
Console.WriteLine("// Static Events");
PrintMembers(ei);
// Static methods.
MethodInfo[] mi = t.GetMethods (BindingFlags.Static |
BindingFlags.NonPublic | BindingFlags.Public);
Console.WriteLine("// Static Methods");
PrintMembers(mi);
// Constructors.
ConstructorInfo[] ci = t.GetConstructors(BindingFlags.Instance |
BindingFlags.NonPublic | BindingFlags.Public);
Console.WriteLine("// Constructors");
PrintMembers(ci);
// Instance fields.
fi = t.GetFields(BindingFlags.Instance | BindingFlags.NonPublic |
BindingFlags.Public);
Console.WriteLine("// Instance Fields");
PrintMembers(fi);
// Instance properites.
pi = t.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic |
BindingFlags.Public);
Console.WriteLine ("// Instance Properties");
PrintMembers(pi);
// Instance events.
ei = t.GetEvents(BindingFlags.Instance | BindingFlags.NonPublic |
BindingFlags.Public);
Console.WriteLine("// Instance Events");
PrintMembers(ei);
// Instance methods.
mi = t.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic
| BindingFlags.Public);
Console.WriteLine("// Instance Methods");
PrintMembers(mi);
Console.WriteLine("\r\nPress ENTER to exit.");
Console.Read();
}
public static void PrintMembers (MemberInfo [] ms)
{
foreach (MemberInfo m in ms)
{
Console.WriteLine ("{0}{1}", " ", m);
}
Console.WriteLine();
}
}
// This program lists all the members of the
// System.IO.BufferedStream class.
using namespace System;
using namespace System::IO;
using namespace System::Reflection;
public ref class ListMembers
{
public:
static void Main()
{
// Specifies the class.
Type^ t = System::IO::BufferedStream::typeid;
Console::WriteLine("Listing all the members (public and non public) of the {0} type", t);
// Lists static fields first.
array<FieldInfo^>^ fi = t->GetFields(BindingFlags::Static |
BindingFlags::NonPublic | BindingFlags::Public);
Console::WriteLine("// Static Fields");
PrintMembers(fi);
// Static properties.
array<PropertyInfo^>^ pi = t->GetProperties(BindingFlags::Static |
BindingFlags::NonPublic | BindingFlags::Public);
Console::WriteLine("// Static Properties");
PrintMembers(pi);
// Static events.
array<EventInfo^>^ ei = t->GetEvents(BindingFlags::Static |
BindingFlags::NonPublic | BindingFlags::Public);
Console::WriteLine("// Static Events");
PrintMembers(ei);
// Static methods.
array<MethodInfo^>^ mi = t->GetMethods (BindingFlags::Static |
BindingFlags::NonPublic | BindingFlags::Public);
Console::WriteLine("// Static Methods");
PrintMembers(mi);
// Constructors.
array<ConstructorInfo^>^ ci = t->GetConstructors(BindingFlags::Instance |
BindingFlags::NonPublic | BindingFlags::Public);
Console::WriteLine("// Constructors");
PrintMembers(ci);
// Instance fields.
fi = t->GetFields(BindingFlags::Instance | BindingFlags::NonPublic |
BindingFlags::Public);
Console::WriteLine("// Instance Fields");
PrintMembers(fi);
// Instance properites.
pi = t->GetProperties(BindingFlags::Instance | BindingFlags::NonPublic |
BindingFlags::Public);
Console::WriteLine ("// Instance Properties");
PrintMembers(pi);
// Instance events.
ei = t->GetEvents(BindingFlags::Instance | BindingFlags::NonPublic |
BindingFlags::Public);
Console::WriteLine("// Instance Events");
PrintMembers(ei);
// Instance methods.
mi = t->GetMethods(BindingFlags::Instance | BindingFlags::NonPublic
| BindingFlags::Public);
Console::WriteLine("// Instance Methods");
PrintMembers(mi);
Console::WriteLine("\r\nPress ENTER to exit.");
Console::Read();
}
static void PrintMembers(array<MemberInfo^>^ ms)
{
for each (MemberInfo^ m in ms)
{
Console::WriteLine ("{0}{1}", " ", m);
}
Console::WriteLine();
}
};
int main()
{
ListMembers::Main();
}