what is reflection in C#, what are the benefit. How to use it to get benifit [closed]

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.


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.


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.


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.


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.