1

How do I make the method shown below to return the name of the property

public class MyClass
{
private string _MyProperty = "TEST";

public string MyProperty
{
    get { return _MyProperty; }
    set { _MyProperty = value; }
}

public string GetName()
{
    return _MyProperty;  // <- should return "MyProperty";
}

}

i don't wana use return "MyProperty" so any alternative?

Usman Masood
  • 1,937
  • 2
  • 17
  • 33
  • May I ask what you're hoping to achieve? – mezoid May 28 '09 at 07:14
  • i have to pass property name to some method that i am creating... basically its reverty previous value back sort of trick. – Usman Masood May 28 '09 at 07:23
  • I understand, but I'm curious as to what possible use the name of the property would be to other code. Perhaps, instead of this being a tricky problem, its a flaw in the design... I can't say that for sure until I know what you need to achieve and if there is a better way. – mezoid May 28 '09 at 07:36

4 Answers4

3

In some way you need to identify the property you want to return its name... If there is just one you may use this:

public string GetName()
{
return this.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)[0].Name;
}
1

As Rythmis said you can't get the name of a property through a member variable. However, here's how to list all properties in a class:

    public class MyClass {
    private string _MyProperty = "TEST";

    public string MyProperty {
        get { return _MyProperty; }
        set { _MyProperty = value; }
    }

    public void GetName() {
        Type t = this.GetType();
        PropertyInfo[] pInfos = t.GetProperties();
        foreach(PropertyInfo x in pInfos)
            Console.WriteLine(x.Name);

    }
}
Ahmed
  • 11,063
  • 16
  • 55
  • 67
0

You can't get the name of a property through a member variable -- the variable just holds a reference to a string, and the string has no knowledge of the property.

What you can do is list all the properties in a class and get their names.

Rytmis
  • 31,467
  • 8
  • 60
  • 69
0

I really cannot see the point of it, but this may be one approach. The method below uses reflection to loop over all properties of the type, fetch the value for each property and use ReferenceEquals to check whether the property references the same object as the one requested:

private string _myProperty = "TEST";
public string MyProperty
{
    get
    {
        return _myProperty;
    }
}

public string GetName(object value)
{
    PropertyInfo[] properties = this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
    for (int i = 0; i < properties.Length; i++)
    {
        object propValue = properties[i].GetValue(this, null);
        if (object.ReferenceEquals(propValue,value))
        {
            return properties[i].Name;
        }
    }

    return null;
}

Console.WriteLine(GetName(this.MyProperty)); // outputs "MyProperty"

This is far from failsafe though. Let's say that the class has two string properties, MyProperty and YourProperty. If we at some point do like this: this.MyProperty = this.YourProperty they will both reference the same string instance, so the above method cannot with certainty determine which property that is the wanted one. As it is written now it will return the first one it finds in the array.

Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343