Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I will be thankful if you guys share me the uses of reflection , that you have implemented in your project. I know about Reflection .. but haven't got chance to use it yet. :(
Just wanted to know all various scenarios where they are used.
Thanks
Posted
Comments
Andreas Gieriet 28-Feb-12 13:19pm    
You can search for elements decorated with attributes and you can traverse the type declarations. You can not directly traverse the IL code though.

With that in mind, you might come up yourself with some ideas, I think...
How about sharing your thougths of what you can think of, to have a starting point! ;-)
Andreas Gieriet 28-Feb-12 13:24pm    
Some items:
- code generators based on declarative code (WCF contracts, ...)
- code analyzer (e.g. FxCop does this - an more...)
- information extractors (e.g. link test methods to requirements, ...)
- ...
Rishikesh_Singh 28-Feb-12 22:15pm    
Thanks Andreas

 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Feb-12 18:05pm    
At least it gives the idea of the domain... a 5.
I tried to outline some main things as I could in a "Quick Answer", please see.
--SA
This is not quite a correct question. The request for "all various scenarios" is just ridiculous. The answer would be: the number of those scenarios in infinite, not no more than countable. And the length of the text of the "Quick Answer" listing each of scenarios would be infinite. Care to try? :-)

Reflection is a huge field of programming. To me, it the question looks the same as someone asked you : I want to know all the scenarios using a class. Or a function. Are you getting the picture?

So, you can only get a general idea. First of all, you need to get an idea of the concept of meta-data. Basically, if you don't know the role of meta-data and Reflection, you have no idea of .NET programming. Ever used serialization? Serialization is not possible about the types of the object being serialized. How can it be possible? The objects keep information of the type, via object.GetType(), and the type System.Type keeps all the information on the type: all members, attributes, all their type, and so on, recursively.

How do you think PropertyGrid can work? You only pass a reference to an object. How this components knows the structure? Through Reflection.

The Reflection is the base of any kinds of plug-ins. Please see my past answers on this topic:
Create WPF Application that uses Reloadable Plugins...[^],
AppDomain refuses to load an assembly[^],
code generating using CodeDom[^],
Create WPF Application that uses Reloadable Plugins...[^],
Dynamically Load User Controls[^],
C# Reflection InvokeMember on existing instance[^],
Projects and DLL's: How to keep them managable?[^].

Please see the overview:
http://en.wikipedia.org/wiki/Metadata[^],
http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29[^].

—SA
 
Share this answer
 
Comments
Rishikesh_Singh 28-Feb-12 21:00pm    
Hi SA thanks for sharing this.. i know no one can give all the scenarios but whatever can be suggested is fine and will help me in thinking about using Reflection in more scenarios where i have initially thought of.
My 5
My alternate tip/trick shows one use: No More Session Variable Misspellings.

Basically, reflection is used in that case to automatically initialize variables to reduce the amount of code. It also ensures you don't mistype something, as all strings are generated based on property names.
 
Share this answer
 
Comments
Wonde Tadesse 28-Feb-12 21:24pm    
Why 2? Here is 5+
Rishikesh_Singh 28-Feb-12 22:14pm    
5 from me too..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900