views:

114

answers:

5

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.

+2  A: 

Reflection is the ability of types to provide information about themselves. For example, an assembly can tell you what it contains, a type can tell you its methods, properties and so on.

Dynamically providing this information is useful in many ways. One simple example to think about is metadata used by web services - when a person "consumes" a web service, a proxy class is generated for their client. This proxy is generated from a WSDL document and that most often is generated from type metadata generated via reflection.

Another simple example is dynamically loading types in order to perform some unit of work. One project I worked on utilized reflection to load "rules" from a database to apply to inputs in the system.

David in Dakota
+1  A: 

Reflection lets your code call methods and properties that you didn't know about when the code was compiled. One of the built in classes that uses this is the XmlSerializer. You can pass it any object you want to convert to XML. It asks the object what all of its properties are using reflection then is able to make an XML document that contains the needed elements to represent the object.

David
+2  A: 

Reflection allows you to write code that can inspect various aspects about the code itself.

It enables you to do simple things like:

  1. Check the type of an object at runtime (simple calls to typeof() for example)

  2. 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:

  1. Loading an assembly at runtime, finding a specific class, determining if it matches a given Interface, and invoking certain members dynamically.

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.

Justin Niessner
+2  A: 

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.

Jim Schubert
A: 

Reflection helps you do metaprogramming, which is unarguably one of the coolest programming techniques. Google for metaprogramming for more information.

Jim