473,386 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Reflecting Class Property Values

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

Nov 17 '05 #1
8 1707
critter <wm*******@yahoo.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
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.
--
Adam Clauss
"critter" <wm*******@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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

Nov 17 '05 #3
So there's no way to do this without instantiating the entire form?

Nov 17 '05 #4
"critter" <wm*******@yahoo.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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?

--
Adam Clauss
Nov 17 '05 #5
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

Nov 17 '05 #6
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/de...nformation.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.
--
Adam Clauss

"critter" <wm*******@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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

Nov 17 '05 #7
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.

Nov 17 '05 #8
Both sound like they'll work, I'll try them out. Thanks for the ideas
guys.

Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: gry | last post by:
I often find myself wanting an instance attribute that can take on only a few fixed symbolic values. (This is less functionality than an enum, since there are no *numbers* associated with the...
0
by: DotNetJunkies User | last post by:
I have to deserialize an XML document to objects and then serialize it back to XML to pass to the stored proc. I am attaching partial code. After this , I also have to serialize Here is the XML :...
10
by: Not Available | last post by:
On the host server: namespace JCart.Common public class JCartConfiguration : IConfigurationSectionHandler private static String dbConnectionString; public static String ConnectionString { get...
2
by: Michael Groeger | last post by:
Hi, is it possible to determine if the PropertyType of a property support IList? We are inspecting all properties of a Type and we need to care about the properties which have a type which...
4
by: MattBell | last post by:
I've tried to search for an answer to this without much success, and I think it's probably a common thing to do: I have a web service I want to accept an XmlDocument as an argument which conforms...
6
by: thomasp | last post by:
For those who gave advice on the shortfalls of my first attempt at writing a vb.net class, Thank You. I hope that I was able to apply some of your advice to this larger atempt. At first I didn' t...
3
by: | last post by:
Hi all, I have a question on reflection Lets say I have a custom object called Address. Now, lets say I have a string variable that holds the name of a variable in the object such as...
0
by: Ryan | last post by:
Hi all, The following code is meant to be used to load a class from an assembly and return a property from it. The class being loaded will always inherit from the base class of MyLicenceBase....
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.