How do I get the property value of an uninstantiated component.

Hi,
I have a problem with Reflection. I want to loop through all of the forms in an assembly. This works fine. But on every form I have some components with a "Caption" property. I can get the names of those components (the assembly names I guess), but I want to get the value of the "Caption" property.
I could get the properties value if I had my form open at the time of requesting it. But now I want to get it when my form is not open. Instantiating all my forms and all my components seems a bit much. Is there a way to get the value of "Caption" at runtime without opening my forms?
The Caption property value is set at design time.
dgbAsked:
Who is Participating?
 
omegaomegaConnect With a Mentor DeveloperCommented:
Hello, dgb,

This sounds like it should be easy, doesn't it.  After all, the Form does it without any trouble each time it is loaded.  Unfortunately, I think that you will need to be able to interpret the IL that is stored in the Form's InitializeComponent method to get what you need.  You can get the IL by using:

    GetMethod("InitializeComponent", BindingFlags.NonPublic Or BindingFlags.Instance)

and using:
   
    GetMethodBody.GetILAsByteArray

on the result.  Interpreting the IL that is returned is beyond my ken.  Others may be able to guide you here, but my guess is that it will be difficult and that your motivation (to not instantiate your form and components) will need to be very strong to make it worth pursuing.  

Cheers,
Randy


0
 
mkobrinConnect With a Mentor Commented:
I doubt very much that this will be possible. If you want to get the values of properties of a class then it will have to be a static class (not going to happen with a form). If the class is not static, it then becomes an object, and you will not be able to get the properties of an object that does not exist.
0
 
orcicCommented:
Try declaring Caption as public const string
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
nepaluzCommented:
Redeclare and DirectCast the object to it's expected object Type.
0
 
dgbAuthor Commented:
Not quite what I wanted to hear, but I guess you guys are right. It is pretty near impossible to get the value of a property of an object without instantiating the object. But instantiating it is exactly what we do not want.
So probably the best solution would indeed be to read from the IL. But we do not have the knowledge nor the time to do this right now. So thanks for your solutions, but I think we'll have to think of something else.
0
 
omegaomegaDeveloperCommented:
Hello, dgb,

Sorry that I couldn't be more helpful.  I also don't know IL (and like you, haven't the time to learn it right now).  With that knowledge the task might be relatively easy, and I'm sure there are people who do know the IL quite well.  It might be worthwhile posting a question in a general dotNet forum asking how to scan the IL to locate the assignment and the string being assigned.  

Good luck,
Randy
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.