Solved

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

Posted on 2011-02-14
6
288 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
Comment Utility
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
Comment Utility
Try declaring Caption as public const string
0
 
LVL 17

Expert Comment

by:nepaluz
Comment Utility
Redeclare and DirectCast the object to it's expected object Type.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 12

Accepted Solution

by:
omegaomega earned 300 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now