I'm looking for a way to do this:
v开发者_如何学Goar instanceOfMyClass = new MyClass();
instanceOfMyClass.AMethod<AType>(x => x.AnotherMethod(y => y.PropertyOfATypeClass));
I can do this but i need to explicitly specify the type for another class, like so:
instanceOfMyClass.AMethod<AType>(x => x.AnotherMethod<AType>(y => y.PropertyOfATypeClass));
Is there a way to do this without needing to rewrite the AType?
The AnotherMethod signature, by now, is:
AnotherMethod<T>(Expression<Func<T, object>>)
where that
<T>
would be the same AType from "AMethod"
The "AnotherMethod" is defined in the same class as "AMethod".
I need the AMethod's signature.
It appears that you have encountered one of the limitations of the C# type inference engine.
The crux of issue is that, in the general case of method group type inference, in order to do type inference on the parameters of a method that takes generic delegates, the compiler needs to know the method signature (to determine which types of delegates there are overloads for); but to determine the method signature, it needs to know the types of your delegate parameters. Thus, its possible for the compiler to get into a cyclical process. To avoid this, the compiler won't even try to do the inference.
For a much more accurate, detailed, and well-written explanation, see this blog post.
And I think the short answer is, no, there's no way to avoid re-specifying the generic type.
精彩评论