mercredi 17 juillet 2019

In C# using Reflection Is there a way to get a getters return property name?

I need to get the name of the private property that is used with an accessor.

public class ClassWithProperty
{
    private string _caption = "A Default caption";

    public string Caption
    {
        get { return _caption; }
        set { if(_caption != value) _caption = value; }
    }
}


Type t = Type.GetType("ClassWithProperty");
         PropertyInfo propInfo = t.GetProperty("Caption");
         MethodInfo[] methInfos = propInfo.GetAccessors(true);

I can loop through the accessors and see the get_Caption and set_Caption but I need to get the "_caption" name.

How can this be done? Thanks!





Aucun commentaire:

Enregistrer un commentaire