presentation on reflection 2 in c#

21
REFLECTION Rohit Vipin Mathews

Upload: harmanbajwa4

Post on 27-Oct-2014

56 views

Category:

Documents


2 download

DESCRIPTION

This presentation details each and every aspect of reflection in C#.Must read it .

TRANSCRIPT

8/16/12

Click to edit Master subtitle style

REFLECTIONRohit Vipin Mathews

8/16/12

Introduction

Reflection is the process of runtime type discovery. is a generic term that describes the ability to inspect and manipulate program elements at runtime. reflection services, we are able to programmatically obtain the same metadata information displayed by ildasm.exe.

Reflection

Using

8/16/12

reflection allows you to: Enumerate Instantiate Execute Find Find

the members of a type a new object

the members of an object

out information about a type out information about an assembly

Inspect Create

the custom attributes applied to a type and compile a new assembly

8/16/12

System.Reflection Namespace

8/16/12

The System.Type Class The

System.Type class defines a number of members that can be used to examine a types metadata, a great number of which return types from the System.Reflection namespace. Type.GetMethods() returns an array of MethodInfo types, Type.GetFields() returns an array of FieldInfo types.

Eg:

8/16/12

Members of System.Type IsAbstract IsArray IsClass IsCOMObject IsEnum IsInterface IsPrimitive IsNestedPrivate

8/16/12

GetConstructors() GetEvents() GetFields() GetInterfaces() GetMembers() GetMethods(). GetNestedTypes() GetProperties()

8/16/12

FindMembers()

This method returns an array of MemberInfo types based on search criteria. This static method returns a Type instance given a string name. This method allows late binding to a given item.

GetType()

InvokeMember()

8/16/12

Using System.type.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. knowledge of the type to be extracted from metadata is not required.

Compile-time

8/16/12

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

Obtaining type information for a type within an external assembly:

8/16/12

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)

8/16/12

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

8/16/12

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

Reflecting on Implemented Interfaces ListInterfaces()

8/16/12

prints out the names of any interfaces supported on the incoming type. call to GetInterfaces() returns an array of System.Types, as interfaces are indeed types.

The

public static void ListInterfaces(Type t) {

8/16/12

displaying various statistics

It indicates whether the type is generic, what the base class is, whether the type is sealed, etc.

public static void ListVariousStats(Type t) { Console.WriteLine("***** Various Statistics *****"); Console.WriteLine("Base class is: {0}",

8/16/12

Dynamically Loading Assemblies The

act of loading external assemblies on demand is known as a dynamic load. defines a class Assembly. Which enables to dynamically load an assembly and discover properties about the assembly. type enables to dynamically load private or shared assemblies, as well as load an assembly located at an

System.Reflection

Assembly

8/16/12

using System; using System.Reflection; using System.IO; namespace ExternalAssemblyReflector { class Program { static void DisplayTypesInAsm(Assembly asm)

8/16/12

static void Main(string[] args) { Console.WriteLine("External Assembly Viewer "); Assembly asm = null; Console.WriteLine("\nEnter an assembly to evaluate"); asmName = Console.ReadLine(); try

8/16/12

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. increases applications Extensibility.

It

8/16/12

System.Activator Class Activator.CreateInstance()

method, which is used to create an instance of a type in late binding.Assembly a = null; try { a = Assembly.Load("CarLibrary"); }

8/16/12

Thank You!!