tags:

views:

137

answers:

5

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?

A: 

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
+3  A: 

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;
}
Javier Suero Santos
But if there is only one property and no other functionality, using a KeyValuePair would make a lot more sense. :)
Rytmis
this was for simple example if i have more than one property than your method will fail...
Usman Masood
+1  A: 

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);

    }
}
Galilyou
+3  A: 

Here's an similar question and answer that uses an Expression for creating a NameOf<> operator.

Erik Hellström
that's a kewl way to do it :) (in that link Erik displayed).
Pure.Krome
thanks Erik... its so good way to achive it... :)
Usman Masood
A: 

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
i thought of it :) but it will fail if my class has same value for two different properties........
Usman Masood