转换System.Func <T>

big*_*gtv 3 c# generics func

我正在重新分解一些使用以下构造的代码.我希望能够做到这一点,所以我可以在Func中有其他参数类型.

IDictionary<string, Func<Employee, string>>
Run Code Online (Sandbox Code Playgroud)

我试过用:

IDictionary<string, Func<Object, string>>
Run Code Online (Sandbox Code Playgroud)

然后把它扔回去:

(IDictionary<string, Func<Object, string>>)myDictionary
Run Code Online (Sandbox Code Playgroud)

但我得到一个例外,说我无法施放

无法转换类型为System.Collections.Generic.Dictionary 2[System.String,System.Func2的对象...

我一直在玩仿制药,但这似乎只是让我觉得我无法在课堂上设置和通用字段

private IDictionary<string, Func<T, string>> lineSpecification;
Run Code Online (Sandbox Code Playgroud)

在圈子四处走动所以任何指针都会受到赞赏.

编辑:

这是我得到的完整异常消息:

System.InvalidCastException : Unable to cast object of type 'System.Collections.Generic.Dictionary`2[System.String,System.Func`2[DataObjects.Employee,System.String]]' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'.
Run Code Online (Sandbox Code Playgroud)

SLa*_*aks 5

这不是类型安全的.

如果这是合法的,如果你传递Car给一个铸造的价值(这真的是一个Func<Employee, string>)会发生什么?