Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-02-14
6
Medium Priority
?
324 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 800 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 12

Accepted Solution

by:
omegaomega earned 1200 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Screencast - Getting to Know the Pipeline
Suggested Courses

963 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