SlideShare uma empresa Scribd logo
1 de 21
REFLECTION
       Rohit Vipin Mathews
INTRODUCTION

 Reflection is the process of runtime type
  discovery.
 Reflection is a generic term that describes
  the ability to inspect and manipulate program
  elements at runtime.
 Using reflection services, we are able to
  programmatically obtain the same metadata
  information displayed by ildasm.exe.
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
SYSTEM.REFLECTION NAMESPACE
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.
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).
   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.
   FindMembers()
       This method returns an array of MemberInfo
        types based on search criteria.
   GetType()
       This static method returns a Type instance given
        a string name.
   InvokeMember()
       This method allows late binding to a given item.
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.
 Compile-time knowledge of the type to be
  extracted from metadata is not required.
   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:
    Type t = Type.GetType("CarLibrary.SportsCar, CarLibrary");
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("");
}
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("");
}
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("");
}
REFLECTING ON IMPLEMENTED INTERFACES

 ListInterfaces() prints out the names of any
  interfaces supported on the incoming type.
 The call to GetInterfaces() returns an array of
  System.Types, as interfaces are indeed types.
public static void ListInterfaces(Type t)
{
  Console.WriteLine("***** Interfaces *****");
  Type[] ifaces = t.GetInterfaces();
  foreach(Type i in ifaces)
  Console.WriteLine("->{0}", i.Name);
}
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}", t.BaseType);
  Console.WriteLine("Is type abstract? {0}", t.IsAbstract);
  Console.WriteLine("Is type sealed? {0}", t.IsSealed);
  Console.WriteLine("Is type generic?
  {0}", t.IsGenericTypeDefinition);
  Console.WriteLine("Is type a class type? {0}", t.IsClass);
}
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.
using System;
using System.Reflection;
using System.IO;
namespace ExternalAssemblyReflector
{
class Program
{
static void DisplayTypesInAsm(Assembly asm)
{
   Console.WriteLine("n Types in Assembly");
   Console.WriteLine("->{0}", asm.FullName);
   Type[] types = asm.GetTypes();
   foreach (Type t in types)
        Console.WriteLine("Type: {0}", t);
}
static void Main(string[] args)
{
    Console.WriteLine("External Assembly Viewer ");
    Assembly asm = null;
    Console.WriteLine("nEnter an assembly to evaluate");
    asmName = Console.ReadLine();
    try
    {
          asm = Assembly.LoadFrom(asmName);
          DisplayTypesInAsm(asm);
    }
    catch
    {
          Console.WriteLine("Sorry, can't find assembly.");
    }
}
}
}
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.
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");
    }
    catch(FileNotFoundException e)
    {
          Console.WriteLine(e.Message);
          Console.ReadLine();
          return;
    }
    Type miniVan = a.GetType("CarLibrary.MiniVan");
    object obj = Activator.CreateInstance(miniVan);
    MethodInfo mi = miniVan.GetMethod("TurboBoost");
    mi.Invoke(obj, null);      //mi.Invoke(obj, paramArray);
Reflection power pointpresentation ppt

Mais conteúdo relacionado

Mais procurados

Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
Benj Del Mundo
 

Mais procurados (20)

Strings in Python
Strings in PythonStrings in Python
Strings in Python
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with python
 
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
 
C# loops
C# loopsC# loops
C# loops
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
11. java methods
11. java methods11. java methods
11. java methods
 
Oop concepts in python
Oop concepts in pythonOop concepts in python
Oop concepts in python
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
CS3391 -OOP -UNIT – II NOTES FINAL.pdf
CS3391 -OOP -UNIT – II  NOTES FINAL.pdfCS3391 -OOP -UNIT – II  NOTES FINAL.pdf
CS3391 -OOP -UNIT – II NOTES FINAL.pdf
 
Python functions
Python functionsPython functions
Python functions
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
 
Structure in c sharp
Structure in c sharpStructure in c sharp
Structure in c sharp
 
JAVA PROGRAMMING - The Collections Framework
JAVA PROGRAMMING - The Collections Framework JAVA PROGRAMMING - The Collections Framework
JAVA PROGRAMMING - The Collections Framework
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and Interface
 
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
 
JAVA THREADS.pdf
JAVA THREADS.pdfJAVA THREADS.pdf
JAVA THREADS.pdf
 
Types of methods in python
Types of methods in pythonTypes of methods in python
Types of methods in python
 
Presentation1
Presentation1Presentation1
Presentation1
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 

Destaque (9)

Attributes & .NET components
Attributes & .NET componentsAttributes & .NET components
Attributes & .NET components
 
.NET Attributes and Reflection - What a Developer Needs to Know...
.NET Attributes and Reflection - What a Developer Needs to Know....NET Attributes and Reflection - What a Developer Needs to Know...
.NET Attributes and Reflection - What a Developer Needs to Know...
 
.NET Reflection
.NET Reflection.NET Reflection
.NET Reflection
 
Bp sesmic interpretation
Bp sesmic interpretationBp sesmic interpretation
Bp sesmic interpretation
 
Basics of seismic interpretation
Basics of seismic interpretationBasics of seismic interpretation
Basics of seismic interpretation
 
Reflection power point
Reflection power pointReflection power point
Reflection power point
 
Reflective Practice Presentation
Reflective Practice PresentationReflective Practice Presentation
Reflective Practice Presentation
 
chapter 10 - refraction of light (na)
chapter 10 -  refraction of light (na)chapter 10 -  refraction of light (na)
chapter 10 - refraction of light (na)
 
Session#1 csharp MTCS
Session#1 csharp MTCSSession#1 csharp MTCS
Session#1 csharp MTCS
 

Semelhante a Reflection power pointpresentation ppt

Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfCreat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
aromanets
 
08 class and object
08   class and object08   class and object
08 class and object
dhrubo kayal
 
03 oo with-c-sharp
03 oo with-c-sharp03 oo with-c-sharp
03 oo with-c-sharp
Naved khan
 
5. c sharp language overview part ii
5. c sharp language overview   part ii5. c sharp language overview   part ii
5. c sharp language overview part ii
Svetlin Nakov
 
Java Sorting CodeImplementing and testing all three sort algorithm.pdf
Java Sorting CodeImplementing and testing all three sort algorithm.pdfJava Sorting CodeImplementing and testing all three sort algorithm.pdf
Java Sorting CodeImplementing and testing all three sort algorithm.pdf
forecastfashions
 
C# Language Overview Part II
C# Language Overview Part IIC# Language Overview Part II
C# Language Overview Part II
Doncho Minkov
 

Semelhante a Reflection power pointpresentation ppt (20)

Reflection
ReflectionReflection
Reflection
 
Reflecting On The Code Dom
Reflecting On The Code DomReflecting On The Code Dom
Reflecting On The Code Dom
 
Reflection
ReflectionReflection
Reflection
 
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfCreat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
 
08 class and object
08   class and object08   class and object
08 class and object
 
Java Reflection
Java ReflectionJava Reflection
Java Reflection
 
03 oo with-c-sharp
03 oo with-c-sharp03 oo with-c-sharp
03 oo with-c-sharp
 
5. c sharp language overview part ii
5. c sharp language overview   part ii5. c sharp language overview   part ii
5. c sharp language overview part ii
 
C# overview part 2
C# overview part 2C# overview part 2
C# overview part 2
 
15reflection in c#
15reflection  in c#15reflection  in c#
15reflection in c#
 
Java Sorting CodeImplementing and testing all three sort algorithm.pdf
Java Sorting CodeImplementing and testing all three sort algorithm.pdfJava Sorting CodeImplementing and testing all three sort algorithm.pdf
Java Sorting CodeImplementing and testing all three sort algorithm.pdf
 
Java class
Java classJava class
Java class
 
Reflection Slides by Zubair Dar
Reflection Slides by Zubair DarReflection Slides by Zubair Dar
Reflection Slides by Zubair Dar
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0
 
C# Language Overview Part II
C# Language Overview Part IIC# Language Overview Part II
C# Language Overview Part II
 
Java interface
Java interfaceJava interface
Java interface
 
Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)
 
Generics
GenericsGenerics
Generics
 
Generics collections
Generics collectionsGenerics collections
Generics collections
 

Último

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Último (20)

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 

Reflection power pointpresentation ppt

  • 1. REFLECTION Rohit Vipin Mathews
  • 2. INTRODUCTION  Reflection is the process of runtime type discovery.  Reflection is a generic term that describes the ability to inspect and manipulate program elements at runtime.  Using reflection services, we are able to programmatically obtain the same metadata information displayed by ildasm.exe.
  • 3. 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
  • 5. 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.
  • 6. 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).
  • 7. 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.
  • 8. FindMembers()  This method returns an array of MemberInfo types based on search criteria.  GetType()  This static method returns a Type instance given a string name.  InvokeMember()  This method allows late binding to a given item.
  • 9. 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.  Compile-time knowledge of the type to be extracted from metadata is not required.
  • 10. 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: Type t = Type.GetType("CarLibrary.SportsCar, CarLibrary");
  • 11. 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(""); }
  • 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(); foreach(FieldInfo field in fi) Console.WriteLine("->{0}", field.Name); Console.WriteLine(""); }
  • 13. 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(""); }
  • 14. REFLECTING ON IMPLEMENTED INTERFACES  ListInterfaces() prints out the names of any interfaces supported on the incoming type.  The call to GetInterfaces() returns an array of System.Types, as interfaces are indeed types. public static void ListInterfaces(Type t) { Console.WriteLine("***** Interfaces *****"); Type[] ifaces = t.GetInterfaces(); foreach(Type i in ifaces) Console.WriteLine("->{0}", i.Name); }
  • 15. 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}", t.BaseType); Console.WriteLine("Is type abstract? {0}", t.IsAbstract); Console.WriteLine("Is type sealed? {0}", t.IsSealed); Console.WriteLine("Is type generic? {0}", t.IsGenericTypeDefinition); Console.WriteLine("Is type a class type? {0}", t.IsClass); }
  • 16. 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.
  • 17. using System; using System.Reflection; using System.IO; namespace ExternalAssemblyReflector { class Program { static void DisplayTypesInAsm(Assembly asm) { Console.WriteLine("n Types in Assembly"); Console.WriteLine("->{0}", asm.FullName); Type[] types = asm.GetTypes(); foreach (Type t in types) Console.WriteLine("Type: {0}", t); }
  • 18. static void Main(string[] args) { Console.WriteLine("External Assembly Viewer "); Assembly asm = null; Console.WriteLine("nEnter an assembly to evaluate"); asmName = Console.ReadLine(); try { asm = Assembly.LoadFrom(asmName); DisplayTypesInAsm(asm); } catch { Console.WriteLine("Sorry, can't find assembly."); } } } }
  • 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.
  • 20. 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"); } catch(FileNotFoundException e) { Console.WriteLine(e.Message); Console.ReadLine(); return; } Type miniVan = a.GetType("CarLibrary.MiniVan"); object obj = Activator.CreateInstance(miniVan); MethodInfo mi = miniVan.GetMethod("TurboBoost"); mi.Invoke(obj, null); //mi.Invoke(obj, paramArray);