Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the name of a property from a property in C# (2.0)

I know I could have an attribute but that's more work than I want to go to... and not general enough.

I want to do something like

class Whotsit
{
    private string testProp = "thingy";

    public string TestProp 
    {
        get { return testProp; }
        set { testProp = value; }
    }

}

...

Whotsit whotsit = new Whotsit();
string value = GetName(whotsit.TestProp); //precise syntax up for grabs..

where I'd expect value to equal "TestProp"

but I can't for the life of me find the right reflection methods to write the GetName method...

EDIT: Why do I want to do this? I have a class to store settings read from a 'name', 'value' table. This is populated by a generalised method based upon reflection. I'd quite like to write the reverse...

/// <summary>
/// Populates an object from a datatable where the rows have columns called NameField and ValueField. 
/// If the property with the 'name' exists, and is not read-only, it is populated from the 
/// valueField. Any other columns in the dataTable are ignored. If there is no property called
/// nameField it is ignored. Any properties of the object not found in the data table retain their
/// original values.
/// </summary>
/// <typeparam name="T">Type of the object to be populated.</typeparam>
/// <param name="toBePopulated">The object to be populated</param>
/// <param name="dataTable">'name, 'value' Data table to populate the object from.</param>
/// <param name="nameField">Field name of the 'name' field'.</param>
/// <param name="valueField">Field name of the 'value' field.</param>
/// <param name="options">Setting to control conversions - e.g. nulls as empty strings.</param>

public static void PopulateFromNameValueDataTable<T>
        (T toBePopulated, System.Data.DataTable dataTable, string nameField, string valueField, PopulateOptions options)
    {
        Type type = typeof(T);
        bool nullStringsAsEmptyString = options == PopulateOptions.NullStringsAsEmptyString;

        foreach (DataRow dataRow in dataTable.Rows)
        {
            string name = dataRow[nameField].ToString();
            System.Reflection.PropertyInfo property = type.GetProperty(name);
            object value = dataRow[valueField];

            if (property != null)
            {
                Type propertyType = property.PropertyType;
                if (nullStringsAsEmptyString && (propertyType == typeof(String)))
                {
                    value = TypeHelper.EmptyStringIfNull(value);
                }
                else
                {
                    value = TypeHelper.DefaultIfNull(value, propertyType);
                }

                property.SetValue(toBePopulated, System.Convert.ChangeType(value, propertyType), null);
            }
        }
    }

FURTHER EDIT: I am just in code, have an instance of Whotsit and I want to get the text string of the 'TestProp' property. It seems kind of weird I know, I can just use the literal "TestProp" - or in the case of my class to datatable function I'd be in a foreach loop of PropertyInfos. I was just curious...

The original code had string constants, which I found clumsy.

like image 530
kpollock Avatar asked Dec 23 '08 12:12

kpollock


People also ask

How do I find the property name and value?

public class myClass { public int a { get; set; } public int b { get; set; } public int c { get; set; } } public void myMethod(myClass data) { Dictionary<string, string> myDict = new Dictionary<string, string>(); Type t = data. GetType(); foreach (PropertyInfo pi in t. GetProperties()) { myDict[pi.Name] = //...

How can I find the class name of a property?

To get names of properties for a specific type use method Type. GetProperties. Method returns array of PropertyInfo objects and the property names are available through PropertyInfo.Name property.

What is property name in C#?

A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they're public data members, but they're special methods called accessors.

How do you get the properties name of an object in typescript?

To dynamically access an object's property: Use keyof typeof obj as the type of the dynamic key, e.g. type ObjectKey = keyof typeof obj; . Use bracket notation to access the object's property, e.g. obj[myVar] .


Video Answer


2 Answers

No, there's nothing to do this. The expression whotsit.TestProp will evaluate the property. What you want is the mythical "infoof" operator:

// I wish...
MemberInfo member = infoof(whotsit.TestProp);

As it is, you can only use reflection to get the property by name - not from code. (Or get all the properties, of course. It still doesn't help you with your sample though.)

One alternative is to use an expression tree:

Expression<Func<string>> = () => whotsit.TestProp;

then examine the expression tree to get the property.

If none of this helps, perhaps you could tell us more about why you want this functionality?

like image 137
Jon Skeet Avatar answered Oct 17 '22 16:10

Jon Skeet


It is possible (without reflection) but only with latest C# 3.0

quick & very very dirty

class Program
{
    static void Main()
    {
        string propertyName = GetName(() => AppDomain.CurrentDomain);
        Console.WriteLine(propertyName); // prints "CurrentDomain"
        Console.ReadLine();
    }

    public static string GetName(Expression<Func<object>> property)
    {
        return property.Body.ToString().Split('.').Last();
    }
}

Update: I've just realized that Jon Skeet (anyone surprised? :) has covered this possibility already but I'll keep my answer here just in case someone is interested in some example to start with.

like image 43
lubos hasko Avatar answered Oct 17 '22 17:10

lubos hasko