Smoke and Mirrors Reflection in C - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Smoke and Mirrors Reflection in C

Description:

Real-world Reflection in .NET - http://www.devx.com/dotnet/Article/31578/1954. Reflection - Dodge Common Performance Pitfalls http://msdn2.microsoft.com/en-us ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 14
Provided by: igork6
Category:

less

Transcript and Presenter's Notes

Title: Smoke and Mirrors Reflection in C


1
Smoke and Mirrors- Reflection in C
Wekoslav Stefanovski
2
What is reflection
Definition - to look back to itself
Run Time Type Information
Discovering class information solely at run time
.net Reflection
Assembly Access based on metadata
3
System.Type
  • Base of all reflection classes and properties -
    primary way to access metadata
  • Represents all kinds of types (class, interface,
    enumeration)
  • GetType() is a Method of System.Object
  • typeof() operator returns Type object
  • A Type is unique per type
  • Only one instance of Type per type per
    application domain

4
MemberInfo class
  • Provides access to member metadata.
  • MemberType propery is useful to determine the
    tipe of a member
  • Ancestor to all other XXXInfo classes
  • Actually an ancestor to Type

5
Other XXXInfo Classes
  • PropertyInfo
  • GetGetMethod GetSetMethod
  • FieldInfo
  • GetValue SetValue
  • MethodInfo
  • Invoke
  • ConstructorInfo
  • EventInfo

6
Attributes
  • An information which marks the elements of code.
  • A class that derives from System.Attribute
  • Multiple points of consumption compiler, CLR,
    debugger, user code
  • Can use both positional and named parameters

7
Using Custom Attributes
  • Type.GetCustomAttributes
  • Attribute.GetCustomAttribute
  • Only way to get information about attributes
    applied to an object in user code

8
System.Activator
  • A generalized way for creating objects at runtime
  • CreateInstance method generic and regular
  • Other Uses

9
Reflection and Generics
  • Difference between regular types, generic types,
    and specialized types
  • Type.IsGenericType
  • Type.IsGenericTypeDefinition
  • Type.MakeGenericType
  • MethodInfo.IsGenericMethod
  • MethodInfo.IsGenericMethodDefinition

10
Reflection.Emit
  • Creating, modifying and running of code in
    runtime
  • Does not use C, uses MSIL
  • Very complex, very powerfull

11
Speed of Reflecting
  • As expected, direct access is much faster than
    reflection
  • Performance hit anywhere from 5 to 150 times
    slower
  • Avoid using lots of reflection in tight loops
  • We should weight if the usefulness in the
    scenario justifies the slowdown.

12
References
  • Generics Terminology in .NET Framework -
    http//blogs.msdn.com/parthopdas/archive/2005/10/2
    1/483463.aspx
  • Hello World.Emit - http//blogs.msdn.com/joelpob/a
    rchive/2004/01/21/61411.aspx
  • Programming C Attributes and Reflection -
    http//www.ondotnet.com/pub/a/dotnet/excerpt/prog_
    csharp_ch18/index.html?page1
  • How Microsoft Uses Reflection -
    http//msdn2.microsoft.com/en-us/magazine/cc163960
    .aspx
  • Real-world Reflection in .NET -
    http//www.devx.com/dotnet/Article/31578/1954
  • Reflection - Dodge Common Performance Pitfalls
    http//msdn2.microsoft.com/en-us/magazine/cc163759
    .aspx
  • .NET Reflection Dynamically Bind Your Data Layer
    -http//msdn.microsoft.com/msdnmag/issues/02/08/N
    ETReflection/default.aspx
  • And many others

13
Questions?
Write a Comment
User Comments (0)
About PowerShow.com