This will not work, because C # generators are fundamentally different from C ++ templates. Created instances of the universal class are created at runtime, while instances of C ++ templates are created at compile time (as far as I know, my C ++ is very rusty). The general Do<T> method must know at compile time one invocation method that can be baked as a result of IL.
A way to achieve this is to use reflection or dynamic (new in C # 4):
class Test<T> { public T Val; public void Do(T val) { Val = val; dynamic dynVal = Val; MainClass.Print(dynVal); } }
With dynamic method search will be performed at runtime. Please note that this is completely dependent on generics and will work equally well in non-standard code.
source share