Briefkasten
Briefkasten

Reputation: 1994

Save Property Expression into Dictionary

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.

Upvotes: 0

Views: 93

Answers (1)

Jon
Jon

Reputation: 437386

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 },

Upvotes: 1

Related Questions