I was reading an article at msdn about reflection but i was not able to understand it even 10% about its benifit, its usage.
Could you please provide some brief overview what is reflection and how can i take benefit from it.
The Crefl API provides runtime access to reflection metadata for C structure declarations with support for arbitrarily nested combinations of: intrinsic, set, enum, struct, union, field (member), array, constant, variable. The Crefl reflection graph database format for portable reflection metadata.
Reflection provides objects (of type Type) that describe assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.
Assembly: Mscorlib.dll. Namespace: System.Reflection. Summary. Defines an Assembly, which is a reusable, versionable, and self-describing building block of a common language runtime application.
Or: typeof(MyType); To get the Type , then when one queries the info about the type e.g. getting properties, fields, attributes etc. they are certainly performing reflection.
Reflection allows you to write code that can inspect various aspects about the code itself.
It enables you to do simple things like:
Check the type of an object at runtime (simple calls to typeof()
for example)
Inspect the Attributes of an object at runtime to change the behavior of a method (the various serialization methods in .NET)
To much more complicated tasks like:
The earlier is much more common usage. The later is helpful to developers working on plug-in architectures for their applications or people who want to swap assemblies at runtime depending on configuration changes.
Reflection is a way for you to programmatically discover Types at runtime. This is very important because .NET languages are strongly-typed. Being able to access that metadata is extremely useful.
A big thing right now (fluent interfaces/adapters) rely heavily on reflection. In particular, static reflection is pretty big. If you want to see specific examples and a good explanation of static reflection, check out:
http://jagregory.com/writings/introduction-to-static-reflection/
http://www.lostechies.com/blogs/gabrielschenker/archive/2009/02/03/dynamic-reflection-versus-static-reflection.aspx
Of course, this a small subset of reflection in general. If you'd like more info about the general use of reflection, check out Apress Pro C# 2008 and the .NET 3.5 Platform, Fourth Edition, Chapter 16. It delves pretty in-depth into the .NET type system and how that is used within libraries and at runtime.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With