Getting the name of the property from which the value was obtained

I would like to know how to get the name of the property that the method parameter value comes from. The code snippet below shows what I want to do:

Person peep = new Person();
Dictionary<object, string> mapping = new Dictionary<object, string>();
mapping[peep.FirstName] = "Name";
Dictionary<string, string> propertyToStringMapping = Convert(mapping);
if (mapping[peep.FirstName] == propertyToStringMapping["FirstName"])
  Console.WriteLine("This is my desired result");

private Dictionary<string, string> Convert(Dictionary<object, string> mapping)
{
   Dictionary<string, string> stringMapping = new Dictionary<string, string>();
   foreach (KeyValuePair<object, string> kvp in mapping)
   {
     //propertyName should eqal "FirstName"
     string propertyName = kvp.Key??????
     stringMapping[propertyName] = kvp.Value;
   }
   return stringMapping;
}

      

0


source to share


3 answers


I think that ultimately you will need to store either the PropertyInfo object associated with that property, or the string representation of the property name in your mapping object. The syntax you have is:

mapping[peep.FirstName] = "Name";

      

Would create a dictionary entry with a key value equal to the value of the peep.FirstName property and the Value equal to "Name".

If you store the property name as a string like:

mapping["FirstName"] = "Name";

      

Then you can use reflection to get the "FirstName" property of your object. However, you have to pass the "peep" object to the Convert function. This seems to be somewhat the opposite of what you want to do.



You can also go crazy with expressions and do something like:

var mapping = new Dictionary<Expression<Action<T>>,string>();
mapping[ p => p.FirstName ] = "Name";

      

Then, in your Convert function, you can check the expression. It would look like this:

private Dictionary<string,string> Convert(Dictionary<Expression<Action<T>>,string> mapping)
{
    var result = new Dictionary<string,string>();
    foreach(var item in mapping)
    {
        LambdaExpression ex = item.Key as LambdaExpression;
        string propertyName = ((MemberExpression)ex.Body).Member.Name;
        string propertyValue = item.Value;
        result.Add(propertyName,proeprtyValue);
    }
    return result;
}

      

This is more or less from the top of the head, so I can have several types of expressions. If there are problems with this implementation, let me know and I will see if I can develop a functional example.

0


source


You cannot do it this way, as the way it works is that C # evaluates the value of the FirstName property by calling its get accessor and passes the value of that to the dictionary indexer. So you have lost the FirstName value. Just like you evaluate 2 + 2. If you write "x = 2 + 2" x will have a value of 4, but there is no way to tell if it was 3 + 1 or 2 + 2 or 5 + (-1 ) or ... which was rated at 4.



+1


source


I don't know much about C #, but I suppose peep is an enum? As far as Java is concerned, you can do:

String propertyName = kvp.key.toString()

      

Maybe something similar in C #?

And even if the peep is not an enumeration: I see no reason why the key should be an arbitrary object? So maybe the solution is precisely to use an enum as the key type?

Also, I don't know what you are trying to do, but usually, I would not recommend that you convert the enum key to a string. What can you do with a string that you can't do with an enumeration either?

0


source







All Articles