/// <summary>
        ///
        /// </summary>
        /// <param name="moduleBuilder"></param>
        /// <param name="proxiedInterfaceType"></param>
        /// <param name="proxiedType"></param>
        /// <param name="way"></param>
        public ProxyTypeGenerator(ModuleBuilder moduleBuilder, Type proxiedInterfaceType, Type proxiedType, ProxyTypeGenerateWay way)
        {
            if (!proxiedInterfaceType.IsInterface)
            {
                throw new InvalidProxiedInterfaceTypeException($"Unable to cast object of type '{proxiedType}' to type '{proxiedInterfaceType}");
            }
            if (!proxiedInterfaceType.IsAssignableFrom(proxiedType))
            {
                throw new InvalidCastException($"Unable to cast object of type '{proxiedType}' to type '{proxiedInterfaceType}");
            }
            if (way == ProxyTypeGenerateWay.ByNewObj &&
                proxiedType.GetConstructors(BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance).Length == 0)
            {
                throw new InvalidProxiedTypeException($"Type {proxiedType} that no public constructor in, cann't use '{nameof(ProxyTypeGenerateWay)}.{ProxyTypeGenerateWay.ByNewObj}'");
            }
            _proxiedInterfaceType = proxiedInterfaceType;
            ProxiedType           = proxiedType;
            Builder = moduleBuilder.DefineType($"{proxiedType.Name}__DynamicProxy{way}", TypeAttributes.Public | TypeAttributes.Sealed, null, new Type[]
            {
                proxiedInterfaceType
            });
            Way = way;

            ProxiedObjField   = Builder.DefineField("_proxiedObj", proxiedInterfaceType, FieldAttributes.Private);
            InterceptorsField = Builder.DefineField(Consts.INTERCEPTORS_FIELD_NAME, typeof(IInterceptor[]), FieldAttributes.Public | FieldAttributes.Static);
        }
예제 #2
0
        public ProxyTypeGenerator(ModuleBuilder moduleBuilder, Type proxiedInterfaceType, Type proxiedType, ProxyTypeGenerateWay way)
        {
            _proxiedInterfaceType = proxiedInterfaceType;
            ProxiedType           = proxiedType;
            Builder = moduleBuilder.DefineType($"{proxiedType.Name}__DynamicProxy{way}", TypeAttributes.Public | TypeAttributes.Sealed, null, new Type[]
            {
                proxiedInterfaceType
            });
            Way = way;

            var proxiedObjField = Builder.DefineField(Consts.PROXIED_OBJECT_FIELD_NAME, proxiedInterfaceType, FieldAttributes.Private);

            AddField(proxiedObjField);
            var interceptorTypesField = Builder.DefineField(Consts.INTERCEPTOR_TYPES_FIELD_NAME, typeof(Type[]), FieldAttributes.Public | FieldAttributes.Static);

            AddField(interceptorTypesField);
        }