views:

618

answers:

7

Why use of Reflection in .net C# code are recommended?

Is it definately a good parctice to use it?

What will the possible situations in a project to take maximum benefit of reflection, can we identify some?

+3  A: 

There's many uses for reflection:

  1. Iterating through properties in an object.
  2. Invoking a method that's defined at runtime.
  3. Many other other on the same vein.

However, one of my favorite uses of reflection is to find properties that have been marked with attributes.

For example, I've written attributes that mark which properties in my classes should be indexed using Lucene. At runtime, I can look at any class, and figure out what fields need to get indexed by just querying the class for "marked" properties.

Esteban Araya
+1  A: 

As mentioned above, performance will take a hit.

Another great advantage is that you can dynamically load assemblies, perform property manipulation even though you may not have the scope to see what to change, etc.

The reasons to use this are plenty. Here is an introduction if you need.

Kyle Rozendo
+2  A: 

Reflection is just a way of investigating objects during run-time. You shouldn't use it if you don't need to do just that.

Stuart Branham
Not sure why you got downvoted, but I fixed that :)
leppie
Indeed. Reflection is an extremely powerful resource but it takes its performance duty, so it should not be used unless really necessary.
Konamiman
A: 

Reflection is commonly used in IoC containers. Let's say you want to register every concrete class the ends with the word "Controller". Reflection makes that a piece of cake.

I've also used reflection to manipulate private fields when unit testing classes.

Scott Muc
+1  A: 

The main value of Reflection is that it can be used to inspect assemblies, types, and members. It's a very powerful tool for determining the contents of an unknown assembly or object and can be used in a wide variety of cases.

Opponents of Reflection will cite that it is slow, which is true when compared to static code execution--however Reflection is used throughout the .NET framework, and provided that it's not abused it can be a very powerful tool in the toolkit.

Some useful applications:

  • Determing dependancies of an assembly

  • Location types which conform to an interface, derive from a base / abstract class, and searching for members by attributes

  • (Smelly) testing - If you depend on a class which is untestable (ie it doesn't allow you to easily build a fake) you can use Reflection to inject fake values within the class--it's not pretty, and not recommended, but it can be a handy tool in a bind.

  • Debugging - dumping out a list of the loaded assemblies, their references, current methods, etc...

STW
Reflection is used by almost all test framework to start with, to find and execute the tests...
Jon Skeet
A: 

Coming from C++ and having needed some simple class hierarchies, I can say that the is keyword is invaluable!

class MenuItem : Item { }

foreach(Item items in parent.ChildItems) {
    if (item is MenuItem) { /* handle differently */ }
}

P.S. Isn't reflection slightly expensive, btw?

Nick Bedford
This is not reflection :)And no, only certain parts in certain use cases are slow.
leppie
My bad, this is type introspection.
Nick Bedford
+2  A: 

You can use reflection to implement a system of plugins for example. You just look for all DLL's in a folder and through reflection check if they implement a certain plugin interface. This is the main purpose for which I used reflection, but I also used it to implement a generic home-brew object serialization, where performance was not the greatest concern.

rslite