Top Banner
Interactive Session on Reflection In C Sharp Classification: Restricted 2012-04-19
19

Reflection in C Sharp

Jan 13, 2015

Download

Education

Harman Bajwa

This presentation provides you with an overview of Reflection Mechanism in C Sharp.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Reflection in C Sharp

Interactive Session on Reflection In C Sharp

Classification: Restricted 2012-04-19

Page 2: Reflection in C Sharp

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-192

Page 3: Reflection in C Sharp

ReflectionName Space to be used System.reflection

• When writing a C# code that uses reflection, the coder can use the type of operator to get the object's type or use the GetType() method to get the type of the current instance.

• Using reflection services, we are able to programmatically obtain the same metadata information displayed by ildasm.exe.

Classification: Restricted 2012-04-193

Page 4: Reflection in C Sharp

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-194

Page 5: Reflection in C Sharp

When use Reflection

• 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-195

Page 6: Reflection in C Sharp

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());

Output of the above code

mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

Classification: Restricted 2012-04-196

Page 7: Reflection in C Sharp

So Reflection allows you to

• Enumerate the members of a type

• Instantiate a new object

• Execute the members of an object

• Find out information about a type

• Find out information about an assembly

• Inspect the custom attributes applied to a type

• Create and compile a new assembly

Classification: Restricted 2012-04-197

Page 8: Reflection in C Sharp

System.Reflection Namespace

Classification: Restricted 2012-04-198

Page 9: Reflection in C Sharp

The System.Type Class

• The System.Type class defines a number of members that can be used to examine a type’s metadata, a great number of which return types from the System.Reflection namespace.

• Eg: Type.GetMethods() returns an array of MethodInfo types, Type.GetFields() returns an array of FieldInfo types.

Classification: Restricted 2012-04-199

Page 10: Reflection in C Sharp

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).

Classification: Restricted 2012-04-1910

Page 11: Reflection in C Sharp

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.

Classification: Restricted 2012-04-1911

Page 12: Reflection in C Sharp

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.

Classification: Restricted 2012-04-1912

Page 13: Reflection in C Sharp

GET TYPE METHOD

• The Type.GetType() method has overloads to allow you to specify two Boolean parameters, one of which controls whether an exception should be thrown if the type cannot be found, and the other of which establishes the case sensitivity of the string.

• Obtaining type information using the static Type.GetType() method:

• Type t = Type.GetType("CarLibrary.SportsCar", false, true);

Classification: Restricted 2012-04-1913

Page 14: Reflection in C Sharp

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("");

}

Classification: Restricted 2012-04-1914

Page 15: Reflection in C Sharp

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();

foreach(FieldInfo field in fi)

Console.WriteLine("->{0}", field.Name);

Console.WriteLine("");

}

Classification: Restricted 2012-04-1915

Page 16: Reflection in C Sharp

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("");

}

Classification: Restricted 2012-04-1916

Page 17: Reflection in C Sharp

Dynamically Loading Assemblies

• The act of loading external assemblies on demand is known as a dynamic load.

• System.Reflection defines a class Assembly. Which enables to dynamically load an assembly and discover properties about the assembly.

• Assembly type enables to dynamically load private or shared assemblies, as well as load an assembly located at an arbitrary location.

Classification: Restricted 2012-04-1917

Page 18: Reflection in C Sharp

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.

Classification: Restricted 2012-04-1918

Page 19: Reflection in C Sharp

Reflection in C #

Harman Aplication Developer

Classification: Restricted 2012-04-1919