Since interfaces in C # cannot contain definitions for statements (or any static methods, for that matter), I believe the answer is no. An alternative is to use a class (cannot be abstract, unfortunately, since static members / operators are no better here than in interfaces). This will allow you to define the implicit conversion operator, and therefore be able to use the type exactly as you specified.
In a class (which you can possibly do virtual if necessary), you would define it somehow like the following.
public class MyClass<T> { public static implicit operator MyClass<T>(Func<T, T> func) { return new MyClass<T>() { MyFunction = func }; } public MyClass() { } public Func<T, T> MyFunction { get; set; } }
Your TestFunction definition in your question should work exactly as you encoded it.
public T TestFunction<T>(IMyInterface myInterface, T value) { return myInterface.MyFunction(value); }
And also call TestFunction :
TestFunction<string>(x => return x + " world", "hello");
This may not be exactly what you are looking for, but nevertheless close enough, and, moreover, you will most likely be able to get the best.
source share