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

后端 未结 4 1774
死守一世寂寞
死守一世寂寞 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:15

    You have to know the T type at compile-time to use it. The T can either be specified at class-level or at method-level.

    class SomeClass {
        public void SomeUtility(Func converter) {
            var myType = converter("foo"); // Already is the T-type that you specified.
        }
    }
    

    or

    public void SomeUtility(Func converter) {
        var myType = converter("foo"); // Already is the T-type that you specified.
    }
    

提交回复
热议问题