I'm trying to write a generic method that supplies parameters and calls a function, like this:
class MyClass {
public int Method(float arg) => 0;
}
TResult Call<T1, TResult>(Func<T1, TResult> func) =>
func(default(T1));
void Main()
{
var m = new MyClass();
var r1 = Call<float, int>(m.Method);
var r2 = Call(m.Method); // CS0411
}
The last line fails to compile with CS0411 . Is there any workaround to get type inference to work here?
Use case: using AutoFixture to generate function call parameters.
Unfortunately no, that's a limitation of type inference in C#. It doesn't really do much with return types, which is what's needed in your case to fully infer the generic arguments to Call<>
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.