0

regarding "Retrieving Property name from lambda expression" https://stackoverflow.com/a/672212/740651 I wondered whether its possible to save an property expression into a dictionary.

I don't want to save the PropertyInfo object it self into the dictionary, because the dictionary should be a static member variable. Therefore I only know the type of the source, but I dont got the instance of it. So i tried the following:

        Dictionary<int, Expression<Func<myfooclass, object>>> dic = 
    new Dictionary<int, Expression<Func<myfooclass, object>>>()
        {
                         { 1, <myfooclass, String> u => u.PropertieFoo },
                         { 2, <myfooclass, int> u => u.SomePropertie },
                         [...]
        };

Have anyone an idea how to solve this issue?

[Edit] I want to specify the properties type in the dictionary.

Community
  • 1
  • 1
Briefkasten
  • 1,964
  • 2
  • 25
  • 53

1 Answers1

1

You don't need to (and cannot do it with that syntax) to specify the generic type arguments when adding values to the dictionary. Simply use

{ 1, u => u.PropertieFoo },
{ 2, u => u.SomePropertie },
Jon
  • 428,835
  • 81
  • 738
  • 806
  • On some properties e.g. when the datatype is bool/enum the lambda expression does something like "Convert(u.PropertieFoo)" which results that (propertyLambda.Body as MemberExpression) is null. Is there a way to set the type of the property value in the dictionary? – Briefkasten Nov 25 '15 at 09:46