public static object createProxyByInterface(Type type, InvocationHandlerInterface handler, bool userCache = true) { if (!userCache || !ProxyDict.TryGetValue(type, out var info)) { var handlerInvokeMethodInfo = typeof(InvocationHandlerInterface).GetMethod("invoke") ?? throw new Exception("不可能的错误:handlerInvokeMethodInfo"); var typeBuilder = createDynamicTypeBuilder(type, null, new[] { type }); var methodInfos = type.GetMethods(); proxyInit(type, typeBuilder, methodInfos, handlerInvokeMethodInfo); info = ProxyDict[type]; if (info.Count == 1) { info.MethodInfos = methodInfos; } } //Type t = info.TypeBuilder.CreateTypeInfo(); return(Activator.CreateInstance(info.TypeBuilder.CreateTypeInfo(), handler, info.MethodInfos) ?? throw new Exception("不同环境此处可能需要改写")); }
/// <summary> /// 通过类创建动态代理 /// </summary> public static T createProxyByType <T>(InvocationHandlerInterface handler, bool userCache = true) { return((T)createProxyByType(typeof(T), handler, userCache)); }