コード例 #1
0
        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());
        }
コード例 #2
0
        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());
        }