I'm not sure if this is exactly what you need, but it allows you to call AnotherMethod from InterfaceMethod without using reflection. However, it still uses Convert.ChangeType.
The idea is to implement a constrained generic class implementation (here Tin). Then you convert the unlimited type T of InterfaceMethod to Tin. Finally, you can call the AnotherMethod method with a limited type. The following works fine with strings.
public interface ITest { T InterfaceMethod<T> (T arg); } public interface ITest2 { U AnotherMethod<U>(U arg) where U : class; } public class Test<Tin> : ITest, ITest2 where Tin : class { public T InterfaceMethod<T> (T arg) { Tin argU = arg as Tin; if (argU != null) { Tin resultU = AnotherMethod(argU); T resultT = (T)Convert.ChangeType(resultU,typeof(T)); return resultT; } return default(T); } public U AnotherMethod<U> (U arg) where U : class { return arg; } }
d - b source share