Hi Steve,
Yes, you can use reflection to get at the value of the property. Here is the code:
public class Class1
{
private string Property_Name = "MyValue";
public string MyValue
{
get
{
return "Radiohead";
}
}
public void ShowPropertyValue()
{
Type myType = this.GetType();
System.Reflection.PropertyInfo myPropertyInfo = myType.GetProperty("MyValue");
Console.WriteLine("The value of {0} is {1}",
this.Property_Name,
myPropertyInfo.GetValue(this,null));
}
}
>I think I've been waiting for this day.
>
>I have the name of a public property stored to a private field. I want to obtain the value stored in the public property. How can I evaluate the contents of the private field to query the correct public property?
>
>Here's an example of what I'm trying to do:
>
>
>public class Class1
>{
> private string Property_Name = "MyValue";
>
> public string MyValue
> {
> get
> {
> return "Radiohead"
> }
> }
>
> public void ShowPropertyValue()
> {
> Console.WriteLine("The value of {0} is {1}",
> this.Property_Name,
> <<WHAT GOES HERE??>>);
> }
>}
>
>
>Note the "WHAT GOES HERE??" in the ShowPropertyValue() method.
>
>Note too that I'm going to need to *reverse* this operation - store a new value to the specified property.
>
>Thanks.
-----------------------------------------
Cathi Gero, CPA
Prenia Software & Consulting Services
Microsoft C# / .NET MVP
Mere Mortals for .NET MVP
cgero@prenia.comwww.prenia.comWeblog: blogs.prenia.com/cathi