Two possible solutions:
Unsafe
public void WorkWith<T>(Action<T> method) { method.Invoke((T)(object)this); }
This is not typical because you can pass it any method that has a single parameter and does not return a value, for example:
WorkWith((string x) => Console.WriteLine(x));
Typical "version" (using common restrictions):
public class MyClass { public void WorkWith<T>(Action<T> method) where T : MyClass { method.Invoke((T)this); } }
The thing is, in order to be able to discard this in T , the compiler wants to make sure that this always applies to T (so the need for a constraint). As shown in the example of an unsafe type, the "classic" (unsafe) solution used with generics goes through the listing to object .
source share