Académique Documents
Professionnel Documents
Culture Documents
In C Sharp
Classification: Restricted 2012-04-19
Reflection
Reflection is the Feature that enables you to obtain the information about a type. All .NET assemblies have metadata information stored about the types defined in modules. This metadata information can be accessed by mechanism called as Reflection. Reflection is the ability of a managed code to read its own metadata for the purpose of finding assemblies, modules and type information at runtime. In other words, reflection provides objects that encapsulate assemblies, modules and types. A program reflects on itself by extracting metadata from its assembly and using that metadata either to inform the user or to modify its own behavior. Reflection is similar to C++ RTTI (Runtime Type Information), but much broader in scope and capability
Classification: Restricted
2012-04-19
Classification: Restricted
2012-04-19
Example of Reflection
Using GetType to obtain type information: C# Code : int i = 42; System.Type type = i.GetType(); System.Console.WriteLine(type); Output of this Code will be System.Int32
Classification: Restricted
2012-04-19
If we want to know assembly information at run time ,then we use reflection. Reflection are used for data binding in .NET Framework. It is also used for testing in .NET Framework.
Classification: Restricted
2012-04-19
Example
In the below example reflection is used to obtain the full name of a loaded assembly: CODE System.Reflection.Assembly o = System.Reflection.Assembly.Load("mscorlib.dll"); System.Console.WriteLine(o.GetName());
Classification: Restricted
2012-04-19
Classification: Restricted
2012-04-19
System.Reflection Namespace
Classification: Restricted
2012-04-19
Classification: Restricted
2012-04-19
Members of System.Type
IsAbstract IsArray IsClass IsCOMObject IsEnum IsInterface IsPrimitive IsNestedPrivate IsNestedPublic IsSealed IsValueType These properties allow you to discover a number of basic traits about the Type you are referring to (e.g., if it is an abstract method, an array, a nested class, and so forth).
10
Classification: Restricted
2012-04-19
Methods
GetConstructors() GetEvents() GetFields() GetInterfaces() GetMembers() GetMethods(). GetNestedTypes() GetProperties() These methods allow you to obtain an array representing the items (interface, method, property, etc.) you are interested in. Each method returns a related array (e.g., GetFields() returns a FieldInfo array, GetMethods() returns a MethodInfo array, etc.). Each of these methods has a singular form (e.g., GetMethod(), GetProperty(), etc.) that allows you to retrieve a specific item by name, rather than an array of all related items.
11
Classification: Restricted
2012-04-19
Methods
GetType() To obtain type information, you may call the static GetType() member of the System.Type class and specify the fully qualified string name of the type to examine. Compile-time knowledge of the type to be extracted from metadata is not required.
12
Classification: Restricted
2012-04-19
13
Classification: Restricted
2012-04-19
Reflecting on Methods
Type.GetMethods() returns an array of System.Reflection.MethodInfo types. // Display method names of type. public static void ListMethods(Type t) { Console.WriteLine("Methods"); MethodInfo[] mi = t.GetMethods(); foreach(MethodInfo m in mi) Console.WriteLine("->{0}", m.Name); Console.WriteLine(""); }
14
Classification: Restricted
2012-04-19
Reflecting on Fields
Type.GetFields() returns an array of System.Reflection.FieldInfo types.
// Display field names of type public static void ListFields(Type t) { Console.WriteLine("Fields"); FieldInfo[] fi = t.GetFields();
15
Classification: Restricted
2012-04-19
Reflecting on Properties
Type. GetProperties() returns an array of System.Reflection. PropertyInfo types. // Display property names of type. public static void ListProps(Type t) { Console.WriteLine("***** Properties *****"); PropertyInfo[] pi = t.GetProperties(); foreach(PropertyInfo prop in pi) Console.WriteLine("->{0}", prop.Name); Console.WriteLine(""); }
16
Classification: Restricted
2012-04-19
17
Classification: Restricted
2012-04-19
Late Binding
Late binding is a technique in which you are able to create an instance of a given type and invoke its members at runtime without having compile-time knowledge of its existence. It increases applications Extensibility.
18
Classification: Restricted
2012-04-19
19
Classification: Restricted
2012-04-19