Solved

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

Posted on 2011-02-14
6
295 Views
Last Modified: 2013-11-26
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.
0
Comment
Question by:dgb
6 Comments
 
LVL 7

Assisted Solution

by:mkobrin
mkobrin earned 200 total points
ID: 34887046
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
 
LVL 7

Expert Comment

by:orcic
ID: 34887057
Try declaring Caption as public const string
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 34887068
Redeclare and DirectCast the object to it's expected object Type.
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 12

Accepted Solution

by:
omegaomega earned 300 total points
ID: 34899346
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
 

Author Closing Comment

by:dgb
ID: 34907433
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
 
LVL 12

Expert Comment

by:omegaomega
ID: 34908634
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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question