Create generic Func from reflection

后端 未结 2 1569
陌清茗
陌清茗 2021-02-10 00:52

I\'ve specified type in a variable: Type hiddenType. I need to create a Func delegate where T is of type specified in mentioned v

2条回答
  •  青春惊慌失措
    2021-02-10 01:05

    Instead of using a Type, you could consider using a generic wrapper, if it's not possible for you to change the GetInstance signature:

    private Func GetTypedInstance()
    {
        return () => (THidden)GetInstance(typeof(THidden));
    }
    

    Then you can just call it with

    GetTypedInstance();
    

    instead of

    GetInstance(typeof(SomeClass));
    

提交回复
热议问题