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?
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?
There's many uses for reflection:
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.
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.
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.
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.
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...
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?
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.