How can I pass in a func with a generic type parameter?

后端 未结 4 1784
死守一世寂寞
死守一世寂寞 2020-12-01 17:59

I like to send a generic type converter function to a method but I can\'t figure out how to do it.

Here\'s invalid syntax that explains what I like to achieve, the p

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 18:28

    public void SomeUtility(Func converter)
    {
        var myType = converter("foo");
    }
    

    and then:

    SomeUtility(arg => new MyType());
    

    The generic type inference will work in this case.

提交回复
热议问题