Reflecting Types within .NET Framework Assemblies
Assembly Class
Assembly myAssembly = Assembly.GetExecutingAssembly();
Type[] assemblytypes = myAssembly.GetTypes();
Module Class
Module[] mymods = a.GetModules();
Module mymodule = mymods[0];
Type[] moduleTypes = mymodule.GetTypes();
Instance of the object
object myObject = new object();
Type objType = myObject.GetType();
Using typeof keyword
Type specificType = typeof(Int32);
Type Class has following Properties
Name | Description |
---|---|
Assembly | Gets the Assembly |
AssemblyQualifiedName | Gets a fully qualified name |
Attributes | Gets the attributes |
BaseType | Gets the Type |
FullName | Gets a fully qualified name |
HasElementType | Indicates whether this Type encompasses another Type |
IsAbstract | Indicates whether this Type refers to an abstract class |
IsByRef | Type is passed by reference |
IsClass | Type refers to aclass |
IsEnum | Type refers to an enumeration |
IsGenericType | Type contains generic parameters |
IsInterface | Type refers to an interface |
IsMarshalByRef | Type is marshaled by reference |
IsNotPublic | Type is not public |
IsPrimitive | Type is a primitive type |
IsPublic | Type is public |
IsSealed | Type is sealed or not inheritable |
IsValueType | Type is a value type |
IsVisible | Type can be accessed outside of its assembly |
Module | Type resides in |
Namespace | Gets the namespace for this Type |
Methods
Name | Description |
---|---|
GetConstructor | Gets a specific ConstructorInfo |
GetConstructors | Gets all the ConstructorInfo |
GetElementType | Gets the Type that this Type encompasses |
GetEvent | Gets a specific EventInfo |
GetEvents | Gets all the EventInfo |
GetField | Gets a specific FieldInfo |
GetFields | Gets all the FieldInfo |
GetInterface | Gets a specific InterfaceInfo |
GetInterfaces | Gets all the InterfaceInfo |
GetMember | Gets a specific MemberInfo |
GetMembers | Gets all the MemberInfo |
GetMethod | Gets a specific MethodInfo |
GetMethods | Gets all the MethodInfo |
GetNestedType | Gets a specific Type object |
GetNestedTypes | Gets all the Type objects |
GetProperty | Gets a specific PropertyInfo object |
GetProperties | Gets all the PropertyInfo objects |
IsInstanceOfType | Tests an object to see if this is an instance of this Type |
IsSubclassOf | Tests to see whether this Type ultimately derives from another type |
foreach (Attribute attr in t.GetCustomAttributes(false))
{
Console.WriteLine(" {0}", attr.GetType().Name);
}
Also, we can get hold of different parts of Type such as methods, properties, fields, and events by utilizing Type class. Each of these methods has its own class which takes the name of the method and adds Info at the end, so we end up having FieldInfo or EventInfo class. These type classes are derived from MemberInfo abstract class. Here is a list of all
MemeberInfo classes
Name | Description that Represents |
---|---|
ConstructorInfo | Constructor |
EventInfo | Event |
FieldInfo | Field |
LocalVariableInfo | Local variable |
MethodBase | Any member |
MethodInfo | Method |
PropertyInfo | Property |
Type | Single type |
foreach (PropertyInfo myprop in t.GetProperties())
{
Console.WriteLine(" {0}", myprop.Name);
}
We can also get Members of the type by using GetMembers which returns an array of MemberInfo objects
foreach (MemberInfo mymember in t.GetMembers())
{
Console.WriteLine("{0}: {1}", mymember.MemberType, mymember.Name);
}
We were able to examine structure of the Type but what if we needed to look at the code inside of each type. In this case we need to use MethodBody object which is going to present us with Intermediate Language (IL)
MethodBody myBody = meth.GetMethodBody();
Console.WriteLine(" MaxStack: {0}", myBody.MaxStackSize);
foreach (LocalVariableInfo local in myBody.LocalVariables)
{
Console.WriteLine("Local Var ({0}): {1}",
local.LocalType, local.LocalIndex);
}
OR we can get bites with actual representation of IL
foreach (Byte myByte in body.GetILAsByteArray())
{
Console.Write("{0:x2} ", myByte);
}
BindingFlags enumeration can be used to manage how members of a type are retrieved with the help of GetMembers methods.
Enumeration
Name | Description |
---|---|
DeclaredOnly | Members directly declared |
Default | No binding flag is used |
FlattenHierarchy | Declared and inherited members |
IgnoreCase | A case-insensitive matching |
Instance | Members that are part of an instance |
NonPublic | Members that are not public |
Public | Members that are public |
Static | Members that are defined as static |
Type myType = typeof(String);
BindingFlags flags = BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance;
MemberInfo[] myMembers = myType.GetMembers(flags);
foreach (MemberInfo myMember in myMembers)
{
Console.WriteLine("Member Name: {0}", myMember.Name);
}