Smoke and Mirrors - Reflection in C#

1,439 views

Published on

This is an introductory presentation on the topic of C# Reflection.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,439
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Smoke and Mirrors - Reflection in C#

  1. 1. Smoke and Mirrors- Reflection in C# Wekoslav Stefanovski
  2. 2. What is reflection Definition - to look back to itself• Fields Run Time Type Information• Methods Discovering class information solely at run time• Properties .net Reflection Assembly Access based on metadata
  3. 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. 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. 5. Other XXXInfo Classes• PropertyInfo – GetGetMethod; GetSetMethod• FieldInfo – GetValue; SetValue• MethodInfo – Invoke• ConstructorInfo• EventInfo
  6. 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. 7. Using Custom Attributes• Type.GetCustomAttributes• Attribute.GetCustomAttribute• Only way to get information about attributes applied to an object in user code
  8. 8. System.Activator• A generalized way for creating objects at runtime• CreateInstance method – generic and regular• Other Uses
  9. 9. Reflection and Generics• Difference between regular types, generic types, and specialized types• Type.IsGenericType• Type.IsGenericTypeDefinition• Type.MakeGenericType• MethodInfo.IsGenericMethod• MethodInfo.IsGenericMethodDefinition
  10. 10. Reflection.Emit• Creating, modifying and running of code in runtime• Does not use C#, uses MSIL• Very complex, very powerfull
  11. 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. 12. References• Generics Terminology in .NET Framework - http://blogs.msdn.com/parthopdas/archive/2005/10/21/483463.aspx• Hello World.Emit - http://blogs.msdn.com/joelpob/archive/2004/01/21/61411.aspx• Programming C#: Attributes and Reflection - http://www.ondotnet.com/pub/a/dotnet/excerpt/prog_csharp_ch18/index.html ?page=1• 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/NETReflection/default.as px• And many others
  13. 13. Questions?

×