SlideShare una empresa de Scribd logo
1 de 19
Interactive Session on Reflection
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




2   Classification: Restricted   2012-04-19
Reflection
Name 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.




3   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




4   Classification: Restricted   2012-04-19
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.




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


Output of the above code
mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089




6   Classification: Restricted   2012-04-19
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




7   Classification: Restricted   2012-04-19
System.Reflection Namespace




8   Classification: Restricted   2012-04-19
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.




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




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();
     foreach(FieldInfo field in fi)
                    Console.WriteLine("->{0}", field.Name);
     Console.WriteLine("");
}



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
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   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
Reflection in C #

Harman
Aplication Developer




19   Classification: Restricted   2012-04-19

Más contenido relacionado

La actualidad más candente

9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in javaNilesh Dalvi
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentationguest11106b
 
Introduction to web programming with JavaScript
Introduction to web programming with JavaScriptIntroduction to web programming with JavaScript
Introduction to web programming with JavaScriptT11 Sessions
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8LivePerson
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in javaCPD INDIA
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScriptShahDhruv21
 
JavaScript - Chapter 10 - Strings and Arrays
 JavaScript - Chapter 10 - Strings and Arrays JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and ArraysWebStackAcademy
 
Asp.net MVC training session
Asp.net MVC training sessionAsp.net MVC training session
Asp.net MVC training sessionHrichi Mohamed
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework Serhat Can
 

La actualidad más candente (20)

9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
 
JAVA OOP
JAVA OOPJAVA OOP
JAVA OOP
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
 
Introduction to web programming with JavaScript
Introduction to web programming with JavaScriptIntroduction to web programming with JavaScript
Introduction to web programming with JavaScript
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
 
An Introduction To REST API
An Introduction To REST APIAn Introduction To REST API
An Introduction To REST API
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
JDBC
JDBCJDBC
JDBC
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
 
JavaScript - Chapter 10 - Strings and Arrays
 JavaScript - Chapter 10 - Strings and Arrays JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and Arrays
 
Asp.net MVC training session
Asp.net MVC training sessionAsp.net MVC training session
Asp.net MVC training session
 
Advanced Cascading Style Sheets
Advanced Cascading Style SheetsAdvanced Cascading Style Sheets
Advanced Cascading Style Sheets
 
Java Streams
Java StreamsJava Streams
Java Streams
 
07 java collection
07 java collection07 java collection
07 java collection
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
 
Introduction to .NET Framework
Introduction to .NET FrameworkIntroduction to .NET Framework
Introduction to .NET Framework
 
Json
JsonJson
Json
 

Similar a Reflection in C Sharp

Reflection Slides by Zubair Dar
Reflection Slides by Zubair DarReflection Slides by Zubair Dar
Reflection Slides by Zubair Darzubairdar6
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Abou Bakr Ashraf
 
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 iiSvetlin Nakov
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application DevelopmentDhaval Kaneria
 
Implementing one feature set in two JavaScript engines (Web Engines Hackfest ...
Implementing one feature set in two JavaScript engines (Web Engines Hackfest ...Implementing one feature set in two JavaScript engines (Web Engines Hackfest ...
Implementing one feature set in two JavaScript engines (Web Engines Hackfest ...Igalia
 
Reflection power pointpresentation ppt
Reflection power pointpresentation pptReflection power pointpresentation ppt
Reflection power pointpresentation pptRohit Vipin Mathews
 
Introduction to design_patterns
Introduction to design_patternsIntroduction to design_patterns
Introduction to design_patternsamitarcade
 
Object-Oriented Programming with C#
Object-Oriented Programming with C#Object-Oriented Programming with C#
Object-Oriented Programming with C#Svetlin Nakov
 
Net framework session01
Net framework session01Net framework session01
Net framework session01Vivek chan
 
Selenium Training .pptx
Selenium Training .pptxSelenium Training .pptx
Selenium Training .pptxSajidTk2
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#SyedUmairAli9
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ Dev Chauhan
 
10 - Encapsulation(object oriented programming)- java . ppt
10 - Encapsulation(object oriented programming)- java . ppt10 - Encapsulation(object oriented programming)- java . ppt
10 - Encapsulation(object oriented programming)- java . pptVhlRddy
 

Similar a Reflection in C Sharp (20)

Reflection
ReflectionReflection
Reflection
 
Reflection Slides by Zubair Dar
Reflection Slides by Zubair DarReflection Slides by Zubair Dar
Reflection Slides by Zubair Dar
 
Reflection
ReflectionReflection
Reflection
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
 
201005 accelerometer and core Location
201005 accelerometer and core Location201005 accelerometer and core Location
201005 accelerometer and core Location
 
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
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
 
Implementing one feature set in two JavaScript engines (Web Engines Hackfest ...
Implementing one feature set in two JavaScript engines (Web Engines Hackfest ...Implementing one feature set in two JavaScript engines (Web Engines Hackfest ...
Implementing one feature set in two JavaScript engines (Web Engines Hackfest ...
 
Reflection power pointpresentation ppt
Reflection power pointpresentation pptReflection power pointpresentation ppt
Reflection power pointpresentation ppt
 
Introduction to design_patterns
Introduction to design_patternsIntroduction to design_patterns
Introduction to design_patterns
 
Object-Oriented Programming with C#
Object-Oriented Programming with C#Object-Oriented Programming with C#
Object-Oriented Programming with C#
 
Net framework session01
Net framework session01Net framework session01
Net framework session01
 
Selenium Training .pptx
Selenium Training .pptxSelenium Training .pptx
Selenium Training .pptx
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#
 
Generic
GenericGeneric
Generic
 
Php oop (1)
Php oop (1)Php oop (1)
Php oop (1)
 
Oops concept
Oops conceptOops concept
Oops concept
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
 
10 - Encapsulation(object oriented programming)- java . ppt
10 - Encapsulation(object oriented programming)- java . ppt10 - Encapsulation(object oriented programming)- java . ppt
10 - Encapsulation(object oriented programming)- java . ppt
 

Último

ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxleah joy valeriano
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 

Último (20)

ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 

Reflection in C Sharp

  • 1. Interactive Session on Reflection In C Sharp Classification: Restricted 2012-04-19
  • 2. 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 2 Classification: Restricted 2012-04-19
  • 3. Reflection Name 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. 3 Classification: Restricted 2012-04-19
  • 4. 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 4 Classification: Restricted 2012-04-19
  • 5. 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. 5 Classification: Restricted 2012-04-19
  • 6. 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 6 Classification: Restricted 2012-04-19
  • 7. 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 7 Classification: Restricted 2012-04-19
  • 8. System.Reflection Namespace 8 Classification: Restricted 2012-04-19
  • 9. 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. 9 Classification: Restricted 2012-04-19
  • 10. 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
  • 11. 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
  • 12. 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. 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); 13 Classification: Restricted 2012-04-19
  • 14. 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
  • 15. 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(""); } 15 Classification: Restricted 2012-04-19
  • 16. 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. 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 Classification: Restricted 2012-04-19
  • 18. 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. Reflection in C # Harman Aplication Developer 19 Classification: Restricted 2012-04-19