How to

get property name using Reflection

Published: 27. October 2011 | Updated: 27. October 2011
License: Microsoft Public License (MS-PL)
Categories: Framework » Reflection
Tags: C# Reflection
Was this snippet helpful for you? YESYES / NONO

Snippet example shows how to get property name at runtime.

Import namespace

using System.Reflection;

Code

public class MyClass
{
    public string Value
    {
        get
        {
            string propertyName = GetPropertyName(MethodBase.GetCurrentMethod());
            ...
        }
        set
        {
            string propertyName = GetPropertyName(MethodBase.GetCurrentMethod());
            ...
        }
    }

    public static string GetPropertyName(MethodBase method)
    {
        return method.Name.Replace("get_", String.Empty).Replace("set_", String.Empty);
    }
}
Send us feedback about this snippet »



Related Snippets: