smoke and mirrors - reflection in c#

13
Smoke and Mirrors - Reflection in C# Wekoslav Stefanovski

Upload: wekoslav-stefanovski

Post on 19-Jun-2015

844 views

Category:

Technology


0 download

DESCRIPTION

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

TRANSCRIPT

  • 1. Smoke and Mirrors- Reflection in C#WekoslavStefanovski

2. What is reflectionDefinition - to look back to itself Fields Run Time Type Information Methods Discovering class informationsolely at run time Properties .net ReflectionAssembly Access based onmetadata 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 perapplication domain 4. MemberInfo class Provides access to member metadata. MemberType propery is useful todetermine 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 theelements of code. A class that derives fromSystem.Attribute Multiple points of consumption compiler, CLR, debugger, user code Can use both positional and namedparameters 7. Using Custom Attributes Type.GetCustomAttributes Attribute.GetCustomAttribute Only way to get information aboutattributes applied to an object in usercode 8. System.Activator A generalized way for creating objectsat runtime CreateInstance method generic andregular 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 ofcode in runtime Does not use C#, uses MSIL Very complex, very powerfull 11. Speed of Reflecting As expected, direct access is muchfaster than reflection Performance hit anywhere from 5 to150 times slower Avoid using lots of reflection in tightloops We should weight if the usefulness inthe scenario justifies the slowdown. 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 Pitfallshttp://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.aspx And many others 13. Questions?