Preparing to MS Exam 70-483 - Using Reflection

The full set of questions I'm trying to cover in time of my preparation to MS Exam 70-483 you can find here.
System.Reflection namespace contains classes which can be used to read metadata or dynamically invoke behaviour from a type, the most useful ones are:
  • Assembly
  • ConstructorInfo
  • EventInfo
  • FieldInfo
  • LocalVariableInfo
  • MemberInfo
  • MethodBase
  • MethodBody
  • MethodInfo
  • Module (a DLL or EXE composing an assembly)
  • ParameterInfo
  • ProperyInfo.

Assembly Class

An assembly is a compiled piece of code (DLL or EXE), so the System.Reflection.Assembly class provides ability to load assemblies, read their metadata and create instances of the types which are contained in them.

Commonly used properties:

Commonly used methods:

Working with Constructors, Methods, Events, Fields and Properties using Reflection

As I mentioned above System.Reflection namespace contains a lot of classes and each of them can be used to gain particular info of this or that part of an object/class. I want to start from studying main aspects of working with methods and constructors.

MethodBase, ConstructorInfo, MethodInfo, MethodBody

First of all I should mention MethodBase class which is an abstract class and is base class for ConstructorInfo and MethodInfo classes. MemberInfo, in turn, is the base class of MethodBase, but I will speak about it a little bit later.
MethodBase class
MethodBase class' properties provides ability to check the method's attributes, access modifiers, module the method belongs to, to find out if the method abstract, static, virtual, final and so on or if the method is hidden by signature, or if the method secutiry critical(defined by CLR).

NOTES
  • IsAssembly is true if the visibility of this method or constructor is exactly described by MethodAttributes.Assembly
  • ReflectedType property retrieves the Type object that was used to obtain this instance of MemberInfo.
  • DeclaringType property will return the class which has declared the method.
    • If the MemberInfo object is a global member (obtained from the Module.GetMethods method, which returns global methods on a module), the returned DeclaringType will be null.
MethodBase class' methods 
  • GetCurrentMethod() - returns a MethodBase object representing the currently executing method.
  • GetCustomAttributes([Type,] bool) - return an array of all custom attributes applied to the method. Boolean value determines to search inheritance chain or not. 
  • GetGenericArguments()
  • GetMethodBody() returns a MethodBody object that provides access to the MSIL stream, local variables, and exceptions for the current method.
    • MethodBody.ExceptionHandlingClauses
    • MethodBody.InitLocals returns true if the method body contains code to initialize local variables
    • MethodBody.MaxStackSize returns maximum items quantity when executing method
    • MethodBody.GetILAsByteArray() returns the MSIL for the method body, as an array of bytes.
  • GetParameters(), GetType()
  • Invoke()

MethodInfo class
MethodInfo class' methods
  • CreateDelegate()
  • GetBaseDefinition()
  • GetGenericMethodDefinition()
  • MakeGenericMethod()
ConstructorInfo class properties and methods are the same as the MethodInfos's class.

MemberInfo, EventInfo, FieldInfo, ParameterInfo, PropertyInfo

As I've mentioned before, MemberInfo is the base class of MethodBase, furthermore, it is the base class of EventInfo, FieldInfo, ParameterInfo and PropertyInfo classes.

MemberInfo class
MemberInfo class' properties (all)
  • CustomAttributes (Gets a collection that contains this member's custom attributes.)
  • DeclaringType (Gets the class that declares this member.)
  • MemberType (method, constructor, event, and so on.)
  • MetadataToken (Gets a value that identifies a metadata element.)
  • Module (Gets the module in which the type that declares the member represented by the current MemberInfo is defined.)
  • Name (Gets the name of the current member.)
  • ReflectedType (Gets the class object that was used to obtain this instance of MemberInfo.)
MemberInfo methods provides ability of getting array of custom attributes, data of custom attributes, check if attribute applied to a member and make shallow copies.


EventInfo class
EventInfo class' properties

FieldInfo class
FieldInfo class' properties and methods
There is nothing special about FieldInfo class, it provides us with obvious methods and properties like GetType(), GetValue(), SetValue(), Attributes, Name, Value, IsPublic and so on, so I will move to LocalVariableInfo class.

LocalVariableInfo class
LocalVariableInfo class' properties and methods




ParameterInfo class

PropertiesInfo class
Properties:
  • Attributes
  • CanRead / CanWrite
  • GetMethod / SetMethod

Methods:
  • GetAccessors()
  • GetConstatnValue() - a literal value associated with a property by a compiler
  • GetGetMethod() / GetSetMethod() 
  • GetValue() / SetValue()

Module


Comments