Wednesday, April 16, 2008

Refelction

Refelction is the mechanism of discovering class information solely at run time.Wondering where it would be useful? Imagine,you are in visual studio IDE (Integrated devolopment environment) and as you type "object." you would see all the methods,properties and events associated with that object.An other example would be an Object browser.The code attached here,loads all assemblies and displays each class,its contructors, methods, properties and events in tree view.The form also hase a command botton (labeled More Reflection) in whose click event I create an object of MyClass1 class based on the name of the class and invoke its methods during run time.

While there are many reasons to use reflection, let me give you a real life example. Say you are building a custom component in .NET that requires a XML file to be used along with your components DLL. You can store the XML file in the /bin directory with your DLL, but how will you access the XML file without knowing the correct path to the /bin directory? I'm sure this is a rhetorical question if you have understand even 10% of what I have wrote so far, but I'll tell you anyways: r e f l e c t i o n. I've created a sample code snippet for you that does just this. Keep reading…

No comments: