Mega Code Archive

 
Categories / C# / Reflection
 

Sets the property value for the given object to the given value

using System; using System.Reflection; public class TestClass    {     private string caption = "A Default caption";     public string Caption     {         get { return caption; }         set          {              if (caption != value)              {                 caption = value;             }         }     } } class TestPropertyInfo {     public static void Main()     {         TestClass t = new TestClass();         Type myType = t.GetType();         PropertyInfo pinfo = myType.GetProperty("Caption");         Console.WriteLine("\nGetValue: " + pinfo.GetValue(t, null));         pinfo.SetValue(t, "This caption has been changed.", null);         Console.WriteLine("GetValue: " + pinfo.GetValue(t, null));     } }