public Type CreateInterfaceProxy(Type @interface) { if ([email protected]) { throw new ArgumentException($"InterfaceType_Must_Be_Interface, {@interface.FullName}", "T"); } TypeBuilder builder = this._moduleBuilder.DefineType($"{@interface.Name}_Proxy", TypeAttributes.Public, typeof(object), new Type[] { @interface, typeof(IProxy) }); ProxyBuilder proxyBuilder = new ProxyBuilder(builder, @interface); proxyBuilder.DefineClassConstructor(); proxyBuilder.DefineProxyMethod(); foreach (var method in @interface.GetMethods().Where(method => !method.IsSpecialName)) { proxyBuilder.DefineInterceptorMethod(method); } foreach (var property in @interface.GetProperties()) { proxyBuilder.DefineProperty(property); } return(builder.CreateTypeInfo()); }
public Type CreateInterfaceProxy(Type @interface) { if ([email protected]) { throw new ArgumentException($"InterfaceType_Must_Be_Interface, {@interface.FullName}", "T"); } var attributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual | MethodAttributes.Final; TypeBuilder builder = this._moduleBuilder.DefineType($"{@interface.Name}_Proxy", TypeAttributes.Public, typeof(object), new Type[] { @interface }); ProxyBuilder proxyBuilder = new ProxyBuilder(builder, @interface); proxyBuilder.DefineClassConstructor(); foreach (var method in @interface.GetMethods().Where(method => !method.IsSpecialName)) { proxyBuilder.DefineInterceptorMethod(method, attributes); } foreach (var property in @interface.GetProperties()) { var parameterTypes = property.GetIndexParameters().Select(it => it.ParameterType).ToArray(); var propertyBuilder = builder.DefineProperty(property.Name, property.Attributes, property.PropertyType, parameterTypes); var getMethod = property.GetMethod; if (null != getMethod) { propertyBuilder.SetGetMethod(proxyBuilder.DefineInterceptorMethod(getMethod, attributes)); } var setMethod = property.SetMethod; if (null != setMethod) { propertyBuilder.SetSetMethod(proxyBuilder.DefineInterceptorMethod(setMethod, attributes)); } } return(builder.CreateTypeInfo()); }