//接口
public interface IClient
public interface IRequest<out T> where T : myResponse;
//抽象类
public abstract class myResponse;
//返回类,其中一个(一个请求参数类对应一个返回类,这样有100多个)
public class myTPsGetResponse : myResponse;
//请求参数类,其中一个
public class myTPsGetRequest : IRequest<myTPsGetResponse>
//实现 ITopClient 接口
public class DefaultClient : IClient
public DefaultClient(string serverUrl, string appKey, string appSecret); //构造函数
public virtual T Execute<T>(IRequest<T> request) where T : myResponse; //方法
public virtual T Execute<T>(IRequest<T> request, string session) where T : myResponse; //方法
//正常调用,请求类实例化和调用请求方法返回结果类
myTPsGetRequest myTPs= new myTPsGetRequest ();
IClient Client = new DefaultClient();
myTPsGetResponse rsp = Client.Execute(myTPs);
现在要求必须以反射来实现整个调用过程:
//反射参数类,没有问题
Type cn = assembly.GetType(className);
object obj = Activator.CreateInstance(cn true);
//反射方法类,问题在下面
Type myType = typeof(DefaultClient);
//这里返回始终null,不传参提示不明确的匹配,传了不报错返回null
var sType = myType.GetMethod("Execute");
//这里入参类型要求为 ITopRequest<out T>,用上面的obj也不行,不知道要怎么传
sType = sType.MakeGenericMethod(obj.GetType());
尝试了N种方法,大部分的类和方法都可以实现反射调用没有问题,唯独这个碰到了上面问题,始终不得其解,主要是如何反射获取方法,获取后如何反射传入 ITopRequest 这个泛型参数调用方法,望指教,谢谢!