public Type CreateClassProxy(Type @class) { if (@class.IsSealed) { throw new ArgumentException($"BaseType_Cannot_Be_Sealed, {@class.FullName}", "TProxy"); } var builder = _moduleBuilder.DefineType($"{@class.Name}_Proxy", TypeAttributes.Public | TypeAttributes.Sealed, @class, new Type[] { typeof(IProxy) }); ProxyBuilder proxyBuilder = new ProxyBuilder(builder, @class); proxyBuilder.DefineSubClassConstructor(); proxyBuilder.DefineProxyMethod(); foreach (var methodInfo in @class.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(method => method.DeclaringType != typeof(object))) { if (!methodInfo.IsSpecialName && !methodInfo.IsFinal && methodInfo.IsVirtual) { proxyBuilder.DefineInterceptorMethod(methodInfo); } } foreach (var property in @class.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.SetProperty | BindingFlags.GetProperty)) { proxyBuilder.DefineProperty(property); } return(builder.CreateTypeInfo()); }
public Type CreateClassProxy(Type @class) { if (@class.IsSealed) { throw new ArgumentException($"BaseType_Cannot_Be_Sealed, {@class.FullName}", "TProxy"); } var builder = _moduleBuilder.DefineType($"{@class.Name}_Proxy", TypeAttributes.Public | TypeAttributes.Sealed, @class); ProxyBuilder proxyBuilder = new ProxyBuilder(builder, @class); proxyBuilder.DefineSubClassConstructor(); foreach (var methodInfo in @class.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(method => method.DeclaringType != typeof(object))) { if (!methodInfo.IsSpecialName && !methodInfo.IsFinal) { var attributes = GetMethodAttributes(methodInfo); if (null != attributes) { proxyBuilder.DefineInterceptorMethod(methodInfo, attributes.Value); } } } foreach (var property in @class.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.SetProperty | BindingFlags.GetProperty)) { var parameterTypes = property.GetIndexParameters().Select(it => it.ParameterType).ToArray(); PropertyBuilder propertyBuilder = builder.DefineProperty(property.Name, property.Attributes, property.PropertyType, parameterTypes); var getMethod = property.GetMethod; if (getMethod != null && !getMethod.IsFinal) { var attributes = GetMethodAttributes(getMethod); if (null != attributes) { propertyBuilder.SetGetMethod(proxyBuilder.DefineInterceptorMethod(getMethod, attributes.Value)); } } var setMethod = property.SetMethod; if (setMethod != null && !setMethod.IsFinal) { var attributes = GetMethodAttributes(setMethod); if (null != attributes) { propertyBuilder.SetSetMethod(proxyBuilder.DefineInterceptorMethod(setMethod, attributes.Value)); } } } return(builder.CreateTypeInfo()); }