Reflecting Class Property Values

C

critter

I am trying to get a value of a property of a class, but not an
instance of the class. Using Reflection, I can find the class in my
assembly, and I can find the property of my class, however, I can't
figure out how to get the value of the property without instantiating
on object of the class type.

Here is a brief example of what I am trying to do. I have a form,
called MyForm. On the form, at design time I place a label and change
the text property of that label to "This is my label". Using reflection
I can find the MyForm type and I can find the label on the form, and I
can find that the label has a property named "Text". The problem is
that I can't figure out how to see that the value of the property named
"Text" is "This is my label".

I'd appreciate any help on determining if this is possible without
instantiating an instance of the type, and if so how?
Thanks,

Warren
 
J

Jon Skeet [C# MVP]

critter said:
I am trying to get a value of a property of a class, but not an
instance of the class. Using Reflection, I can find the class in my
assembly, and I can find the property of my class, however, I can't
figure out how to get the value of the property without instantiating
on object of the class type.

Unless the property is static, it *has* no property value.
Here is a brief example of what I am trying to do. I have a form,
called MyForm. On the form, at design time I place a label and change
the text property of that label to "This is my label". Using reflection
I can find the MyForm type and I can find the label on the form, and I
can find that the label has a property named "Text". The problem is
that I can't figure out how to see that the value of the property named
"Text" is "This is my label".

You need to pass in the reference to the instance of the label. After
all, it is the Text property of that particular label that you are
interested in.
 
A

Adam Clauss

Properties exist on a per-instance level (just like other non-static
variables and methods) - there is no way to retrieve a instance value
without an instance.

Think about it this way, you want to find the control's Text property value,
but you didn't tell it which control. Each control has it's own independent
Text property.
 
A

Adam Clauss

critter said:
So there's no way to do this without instantiating the entire form?

No there isn't. Otherwise how does it know what form (and what control in
that form) you want to get the value from?
 
C

critter

Ok, I'm an idiot :) I forgot the label's text being set to "This is my
text" is done in the InitializeComponent() method portion of the .cs
file, which VS conveniently hides, and is not stored as part of the
type definition.

What I'm trying to accomplish is creating forms with End-User definable
labels. However, I don't want the user to be able to change any and
every label, just certain ones. Manually maintaining and updating a
list of the labels that can and can't be changed by the user is a major
pain in the neck. I was hoping to place some kind of marker on the
label components (either in the text property or the tag property) that
would allow me to use reflection to dynamically create a list of labels
that the user can change. I would like to do this, if possible, without
instantiating a copy of every form in the application, just to build a
list of labels that can be modified.

If there is a better way to accomplish end-user definable labels, I'm
open to suggestions.
Thanks,

Warren
 
A

Adam Clauss

OK - I'm taking a stab here cause I've never actually used Attributes
before, but if my understanding is correct, they are exactly what you want.

Take a look at:
http://msdn.microsoft.com/library/d.../html/vclrfretrievingattributeinformation.asp

Basically, you define some attribute (UserDefinable or something) derived
from System.Attribute. Unlike the example in the link (Author), you might
not even need any fields in your attribute - just testing for the presence
of this attribute might be enough.

Then, any label you want user-definable could be declared as:
[UserDefinable]
public Label anEditableLabel;

public Label anUneditableLabel;

Then, you can use Reflection just like you are currently doing and call
Attribute.GetCustomAttribute(fieldInfo, typeof(UserDefineable))

If that returns non-null, then the label is user-definable. If it returns
null, it is not.

Again, take that with a grain of salt since I haven't used attributes, but
that seems like it would do what you want.
 
C

Chris Dunaway

OK, try this:

Click on the label you with to have user definable. In the Property
box, expand the Dynamic Properties item and select the Advanced item.
When you click the button next to Advance, it will present a list of
properties. Check the Text property of the label.

Now there will be an entry in the App.config for that property and it
will be set when the app starts.

Perhaps that will help you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top