Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Hey, is this where I'm gonna use REFLECTION?
Message
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
00851624
Message ID:
00852858
Vues:
12
This message has been marked as a message which has helped to the initial question of the thread.
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.com
www.prenia.com
Weblog: blogs.prenia.com/cathi
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform