Get property name inside setter

.net, c#, properties, reflection

Solution

public static int Dummy {
    get {
        var propertyName = MethodBase.GetCurrentMethod().Name.Substring(4);
        Console.WriteLine(propertyName);
        return 0;
    }
}

Problem

I want to preserve a property between postbacks in an ASP.Net application. Currently doing this: ``` public int MyIndex { get { return (int)Session[ToString() + "MyIndex"]; } } ``` but would prefer something like: ``` public int MyIndex { get { return (int)Session[ToString() + #code_that_returns_property_name#]; } } ``` Setter omitted, but it just pushes value into Session using the same string. Is there some way to use reflection, or a different better solution?

Original source

Related problems