Solved

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

Posted on 2011-02-14
6
304 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

628 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