c#Dictionary with Func <string,T> als values

sta*_*ata 0 c# generics dictionary

我想把Func放在T是泛型类型作为字典中的值.所以基本上我想做的事情如下:

 Dictionary<string, Func<MyObject, T>> _sortMappings = 
                new Dictionary<string, Func<MyObject, T>>()
    {
        { "Name", (b) => b.Name }, // name is a string            
        { "Length", (b) => b.Length }, // length is an int
        { "Date", (b) => b.Date } // a datetime object
    };
Run Code Online (Sandbox Code Playgroud)

这是否有意义,这可能吗?

Sel*_*enç 5

Func<MyObject, T>你需要使用它是不可能的Func<MyObject, object>.T不能同时超过一种类型.所以,你需要找到一个共同的类型string,intDateTime这是object.