Is there a delegate available for properties in C#?

后端 未结 3 1558
无人及你
无人及你 2020-12-28 10:04

Given the following class:

class TestClass {
  public void SetValue(int value) { Value = value; }
  public int Value { get; set; }
}

I can

3条回答
  •  礼貌的吻别
    2020-12-28 10:15

    Properties are really wrappers around methods in .Net, so using reflection you should be able to get the delegate (set_PROPERTY and get_PROPERTY) and then execute them...

    See System.Reflection.PropertyInfo

    If has two methods which you can use to get/ set the value - GetGetMethod and GetSetMethod.

    So you could write:

    var propertyInfo = typeof (TestClass).GetProperty ("Value");
    
    var setMethod = property.GetSetMethod (); // This will return a MethodInfo class.
    

提交回复
热议问题