SlideShare une entreprise Scribd logo
1  sur  10
Reflection
 Code Review 8/16/12
Where is this info?
METADATA!
Type Reflection
In the .NET universe, reflection is the process of runtime type
discovery

System.Reflection namespace

    Assembly, AssemblyName, EventInfo, FieldInfo, MethodInfo,
    ParameterInfo and PropertyInfo

System.Type class

    GetConstructors(), GetEvents(), GetFields(), GetInterfaces(),
    GetMembers(), GetMethods(), GetProperties(), InvokeMember()
Reflect the type info
Obtain a type reference using System.Type.GetType()
   Type t = Type.GetType (typeName);
Reflecting on methods
   MethodInfo[] mi = t.GetMethods();
Reflecting on Fields and properties
   FieldInfo[] fi = t.GetFields();
   PropertyInfo[] pi = t.GetProperties();
Dynamic Loading
Dynamic load
  loading external assemblies on
  demand
  Can use Assembly.Load() or
  Assembly.LoadFrom()
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
Assembly a = Assembly.Load(“CarLibrary”);Type miniVan = a.GetType
                       (“CarLibrary.MiniVan”);
           object obj = Activator.CreateInstance(miniVan);
        MethodInfo mi = miniVan.GetMethod(“TurboBoost”);
                          mi.Invoke(obj, null);
Attributed programming

Using attributes, programmers can
embed additional metadata into an
assembly
 .NET attributes are class types that
extend the abstract System.Attribute
base class
Attribute Consumers

.Net Compiler
.Net base libraries (such as Serialize())
Custom Code
Predefined attributes
[CLSCompliant]
[DllImport]
[Obsolete]
[Serializable]
[NonSerialized]
[WebMethod]
Building Custom Attributes


public sealed class VehicleDescriptionAttribute: System.Attribute
{
      private string msgData;
      public VehicleDescriptionAttribute (string description) {
            msgData = description;}
}

Contenu connexe

Tendances

Adding new type to Druid
Adding new type to DruidAdding new type to Druid
Adding new type to DruidNavis Ryu
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java pptkunal kishore
 
constructor and destructor
constructor and destructorconstructor and destructor
constructor and destructorVENNILAV6
 
It seminar-xml serialization
It seminar-xml serializationIt seminar-xml serialization
It seminar-xml serializationPriyojit Mondal
 

Tendances (6)

Unit ii
Unit   iiUnit   ii
Unit ii
 
Adding new type to Druid
Adding new type to DruidAdding new type to Druid
Adding new type to Druid
 
Java constructors
Java constructorsJava constructors
Java constructors
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java ppt
 
constructor and destructor
constructor and destructorconstructor and destructor
constructor and destructor
 
It seminar-xml serialization
It seminar-xml serializationIt seminar-xml serialization
It seminar-xml serialization
 

En vedette

The Behavior of light on different substances
The Behavior of light on different substances The Behavior of light on different substances
The Behavior of light on different substances Nikhil AV
 
Reflection And Refraction
Reflection And RefractionReflection And Refraction
Reflection And Refractioneliseb
 
Pstti light day & night
Pstti light  day & nightPstti light  day & night
Pstti light day & nightPSTTI
 
Green roof system construction
Green roof system constructionGreen roof system construction
Green roof system constructionbronzecs
 
Light sources its advantages & disadvantages
Light sources its advantages & disadvantagesLight sources its advantages & disadvantages
Light sources its advantages & disadvantagesOPTOM FASLU MUHAMMED
 
Solar passive architecture
Solar passive architectureSolar passive architecture
Solar passive architectureskullcrusher
 
Reflection and Refraction
Reflection and RefractionReflection and Refraction
Reflection and Refractionmeenng
 
Ventilation: Basic Principles
Ventilation: Basic PrinciplesVentilation: Basic Principles
Ventilation: Basic PrinciplesJamie Ranse
 
Basic Mechanical Ventilation
Basic Mechanical VentilationBasic Mechanical Ventilation
Basic Mechanical VentilationAndrew Ferguson
 
Sun Shading Devices
Sun Shading DevicesSun Shading Devices
Sun Shading DevicesPrabal Dahal
 

En vedette (16)

The Behavior of light on different substances
The Behavior of light on different substances The Behavior of light on different substances
The Behavior of light on different substances
 
Behavior of light
Behavior of lightBehavior of light
Behavior of light
 
Building Ventilation
Building VentilationBuilding Ventilation
Building Ventilation
 
Reflection And Refraction
Reflection And RefractionReflection And Refraction
Reflection And Refraction
 
Wind tunnel
Wind tunnelWind tunnel
Wind tunnel
 
Pstti light day & night
Pstti light  day & nightPstti light  day & night
Pstti light day & night
 
Green roof system construction
Green roof system constructionGreen roof system construction
Green roof system construction
 
Light sources its advantages & disadvantages
Light sources its advantages & disadvantagesLight sources its advantages & disadvantages
Light sources its advantages & disadvantages
 
Dbn163 # 04. macro micro climate
Dbn163 # 04. macro micro climateDbn163 # 04. macro micro climate
Dbn163 # 04. macro micro climate
 
Environmental science
Environmental scienceEnvironmental science
Environmental science
 
Nature of light (2)
Nature of light (2)Nature of light (2)
Nature of light (2)
 
Solar passive architecture
Solar passive architectureSolar passive architecture
Solar passive architecture
 
Reflection and Refraction
Reflection and RefractionReflection and Refraction
Reflection and Refraction
 
Ventilation: Basic Principles
Ventilation: Basic PrinciplesVentilation: Basic Principles
Ventilation: Basic Principles
 
Basic Mechanical Ventilation
Basic Mechanical VentilationBasic Mechanical Ventilation
Basic Mechanical Ventilation
 
Sun Shading Devices
Sun Shading DevicesSun Shading Devices
Sun Shading Devices
 

Similaire à Reflection

Reflecting On The Code Dom
Reflecting On The Code DomReflecting On The Code Dom
Reflecting On The Code DomNick Harrison
 
Understanding Reflection
Understanding ReflectionUnderstanding Reflection
Understanding ReflectionTamir Khason
 
Attributes & .NET components
Attributes & .NET componentsAttributes & .NET components
Attributes & .NET componentsBình Trọng Án
 
Object Oriented Code RE with HexraysCodeXplorer
Object Oriented Code RE with HexraysCodeXplorerObject Oriented Code RE with HexraysCodeXplorer
Object Oriented Code RE with HexraysCodeXplorerAlex Matrosov
 
NNUG Certification Presentation
NNUG Certification PresentationNNUG Certification Presentation
NNUG Certification PresentationNiall Merrigan
 
03 oo with-c-sharp
03 oo with-c-sharp03 oo with-c-sharp
03 oo with-c-sharpNaved khan
 
Use Eclipse technologies to build a modern embedded IDE
Use Eclipse technologies to build a modern embedded IDEUse Eclipse technologies to build a modern embedded IDE
Use Eclipse technologies to build a modern embedded IDEBenjamin Cabé
 
13 iec t1_s1_oo_ps_session_19
13 iec t1_s1_oo_ps_session_1913 iec t1_s1_oo_ps_session_19
13 iec t1_s1_oo_ps_session_19Niit Care
 
Net framework session03
Net framework session03Net framework session03
Net framework session03Vivek chan
 
Intake 38 data access 5
Intake 38 data access 5Intake 38 data access 5
Intake 38 data access 5Mahmoud Ouf
 
Reflection Slides by Zubair Dar
Reflection Slides by Zubair DarReflection Slides by Zubair Dar
Reflection Slides by Zubair Darzubairdar6
 
Windows 8 für .net Entwickler
Windows 8 für .net EntwicklerWindows 8 für .net Entwickler
Windows 8 für .net EntwicklerPatric Boscolo
 
Learn about dot net attributes
Learn about dot net attributesLearn about dot net attributes
Learn about dot net attributessonia merchant
 

Similaire à Reflection (20)

Reflection in C#
Reflection in C#Reflection in C#
Reflection in C#
 
Module 15 attributes
Module 15 attributesModule 15 attributes
Module 15 attributes
 
Reflecting On The Code Dom
Reflecting On The Code DomReflecting On The Code Dom
Reflecting On The Code Dom
 
Understanding Reflection
Understanding ReflectionUnderstanding Reflection
Understanding Reflection
 
.NET Reflection
.NET Reflection.NET Reflection
.NET Reflection
 
Attributes & .NET components
Attributes & .NET componentsAttributes & .NET components
Attributes & .NET components
 
Reflection
ReflectionReflection
Reflection
 
Object Oriented Code RE with HexraysCodeXplorer
Object Oriented Code RE with HexraysCodeXplorerObject Oriented Code RE with HexraysCodeXplorer
Object Oriented Code RE with HexraysCodeXplorer
 
NNUG Certification Presentation
NNUG Certification PresentationNNUG Certification Presentation
NNUG Certification Presentation
 
03 oo with-c-sharp
03 oo with-c-sharp03 oo with-c-sharp
03 oo with-c-sharp
 
SYCL 1.2.1 Reference Card
SYCL 1.2.1 Reference CardSYCL 1.2.1 Reference Card
SYCL 1.2.1 Reference Card
 
Use Eclipse technologies to build a modern embedded IDE
Use Eclipse technologies to build a modern embedded IDEUse Eclipse technologies to build a modern embedded IDE
Use Eclipse technologies to build a modern embedded IDE
 
13 iec t1_s1_oo_ps_session_19
13 iec t1_s1_oo_ps_session_1913 iec t1_s1_oo_ps_session_19
13 iec t1_s1_oo_ps_session_19
 
Net framework session03
Net framework session03Net framework session03
Net framework session03
 
Intake 38 data access 5
Intake 38 data access 5Intake 38 data access 5
Intake 38 data access 5
 
Reflection Slides by Zubair Dar
Reflection Slides by Zubair DarReflection Slides by Zubair Dar
Reflection Slides by Zubair Dar
 
Windows 8 für .net Entwickler
Windows 8 für .net EntwicklerWindows 8 für .net Entwickler
Windows 8 für .net Entwickler
 
Tdd,Ioc
Tdd,IocTdd,Ioc
Tdd,Ioc
 
Learn about dot net attributes
Learn about dot net attributesLearn about dot net attributes
Learn about dot net attributes
 
Intake 37 ef2
Intake 37 ef2Intake 37 ef2
Intake 37 ef2
 

Plus de Chester Hartin

Plus de Chester Hartin (8)

Xamarin 101
Xamarin 101Xamarin 101
Xamarin 101
 
Proxy pattern
Proxy patternProxy pattern
Proxy pattern
 
Asynchronous programming
Asynchronous programmingAsynchronous programming
Asynchronous programming
 
Elapsed time
Elapsed timeElapsed time
Elapsed time
 
Dependency injection
Dependency injectionDependency injection
Dependency injection
 
Map kit
Map kitMap kit
Map kit
 
Hash tables
Hash tablesHash tables
Hash tables
 
Parallel extensions
Parallel extensionsParallel extensions
Parallel extensions
 

Reflection

  • 2. Where is this info? METADATA!
  • 3. Type Reflection In the .NET universe, reflection is the process of runtime type discovery System.Reflection namespace Assembly, AssemblyName, EventInfo, FieldInfo, MethodInfo, ParameterInfo and PropertyInfo System.Type class GetConstructors(), GetEvents(), GetFields(), GetInterfaces(), GetMembers(), GetMethods(), GetProperties(), InvokeMember()
  • 4. Reflect the type info Obtain a type reference using System.Type.GetType() Type t = Type.GetType (typeName); Reflecting on methods MethodInfo[] mi = t.GetMethods(); Reflecting on Fields and properties FieldInfo[] fi = t.GetFields(); PropertyInfo[] pi = t.GetProperties();
  • 5. Dynamic Loading Dynamic load loading external assemblies on demand Can use Assembly.Load() or Assembly.LoadFrom()
  • 6. 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 Assembly a = Assembly.Load(“CarLibrary”);Type miniVan = a.GetType (“CarLibrary.MiniVan”); object obj = Activator.CreateInstance(miniVan); MethodInfo mi = miniVan.GetMethod(“TurboBoost”); mi.Invoke(obj, null);
  • 7. Attributed programming Using attributes, programmers can embed additional metadata into an assembly .NET attributes are class types that extend the abstract System.Attribute base class
  • 8. Attribute Consumers .Net Compiler .Net base libraries (such as Serialize()) Custom Code
  • 10. Building Custom Attributes public sealed class VehicleDescriptionAttribute: System.Attribute { private string msgData; public VehicleDescriptionAttribute (string description) { msgData = description;} }