示例#1
0
        /// <summary>
        /// Generates a proxy template.
        /// </summary>
        /// <param name="proxyDefinition">The proxy definition.</param>
        /// <returns>The proxy template.</returns>
        private IProxyTemplate GenerateProxyTemplate(IProxyDefinition proxyDefinition)
        {
            var typeBuilder    = _typeBuilderFactory.CreateBuilder(proxyDefinition.ParentType);
            var proxyGenerator = new ProxyGenerator(typeBuilder, _interceptionFilter);

            return(proxyGenerator.GenerateProxyTemplate(proxyDefinition));
        }
示例#2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ProxyTemplate"/> class.
        /// </summary>
        /// <param name="proxyDefinition">The proxy definition.</param>
        /// <param name="implementationType">The implementation type.</param>
        /// <param name="eventInfos">The event informations.</param>
        /// <param name="propertyInfos">The property informations.</param>
        /// <param name="methodInfos">The method informations.</param>
        public ProxyTemplate(IProxyDefinition proxyDefinition, Type implementationType, ICollection <EventInfo> eventInfos, ICollection <PropertyInfo> propertyInfos, ICollection <MethodInfo> methodInfos)
        {
            if (proxyDefinition == null)
            {
                throw new ArgumentNullException("proxyDefinition");
            }

            if (implementationType == null)
            {
                throw new ArgumentNullException("implementationType");
            }

            if (eventInfos == null)
            {
                throw new ArgumentNullException("eventInfos");
            }

            if (propertyInfos == null)
            {
                throw new ArgumentNullException("propertyInfos");
            }

            if (methodInfos == null)
            {
                throw new ArgumentNullException("methodInfos");
            }

            _proxyDefinition    = proxyDefinition;
            _implementationType = implementationType;
            _eventInfos         = eventInfos;
            _propertyInfos      = propertyInfos;
            _methodInfos        = methodInfos;
        }
示例#3
0
        /// <summary>
        /// Creates a proxyfied interface according to the given definition.
        /// </summary>
        /// <param name="definition">Definition of the proxy to build.</param>
        /// <param name="initialImplementation">Optional first and available implementation.</param>
        /// <returns></returns>
        internal static ServiceProxyBase CreateProxy(IProxyDefinition definition)
        {
            Debug.Assert(definition.TypeInterface.IsInterface, "This check MUST be done by ProxyDefinition implementation.");

            string dynamicTypeName = String.Format("{0}_Proxy_{1}", definition.TypeInterface.Name, Interlocked.Increment(ref _typeID));

            // Defines a public sealed class that implements typeInterface only.
            TypeBuilder typeBuilder = _moduleBuilder.DefineType(
                dynamicTypeName,
                TypeAttributes.Class | TypeAttributes.Sealed,
                definition.ProxyBase,
                new Type[] { definition.TypeInterface });

            // This defines the IService<typeInterface> interface.
            if (definition.IsDynamicService)
            {
                // Our proxy object will implement both typeInterface and IService<typeInterface> interfaces.
                Type serviceInterfaceType = typeof(IService <>).MakeGenericType(new Type[] { definition.TypeInterface });
                typeBuilder.AddInterfaceImplementation(serviceInterfaceType);
            }

            ProxyGenerator pg = new ProxyGenerator(typeBuilder, definition);

            pg.DefineConstructor();

            pg.DefineServiceProperty();

            pg.DefineImplementationProperty();

            pg.ImplementInterface();

            object unavailableImpl = CreateUnavailableImplementation(definition.TypeInterface, dynamicTypeName + "_UN");

            return(pg.Finalize(unavailableImpl));
        }
示例#4
0
 public ProxyGenerator(TypeBuilder typeBuilder, IProxyDefinition definition)
 {
     _typeBuilder = typeBuilder;
     _definition  = definition;
     // Define a member variable to hold the implementation
     _implField        = typeBuilder.DefineField("_impl", definition.TypeInterface, FieldAttributes.Private);
     _processedMethods = new HashSet <MethodInfo>();
     _mRefs            = new List <MethodInfo>();
     _eRefs            = new List <EventInfo>();
 }
示例#5
0
        /// <summary>
        /// Generates a proxy template based on the specified proxy definition.
        /// </summary>
        /// <param name="proxyDefinition">The proxy definition.</param>
        /// <returns>The proxy template.</returns>
        public IProxyTemplate GenerateProxyTemplate(IProxyDefinition proxyDefinition)
        {
            if (proxyDefinition == null)
                throw new ArgumentNullException("proxyDefinition");

            // Build type.
            proxyDefinition.AcceptVisitor(this);

            // Create type.
            var type = _typeBuilder.CreateType();

            return new ProxyTemplate(proxyDefinition, type, _eventInfos, _propertyInfos, _methodInfos);
        }
示例#6
0
        public IProxyTemplate GenerateProxyTemplate(IProxyDefinition proxyDefinition)
        {
            if (proxyDefinition == null)
            {
                throw new ArgumentNullException("proxyDefinition");
            }

            //这一步就是用于收集要切的方法和属性、事件
            proxyDefinition.AcceptVisitor(this);

            var type = _typeBuilder.CreateType();

            return(new ProxyTemplate(proxyDefinition, type, _eventInfos, _propertyInfos, _methodInfos));
        }
示例#7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ProxyTemplate"/> class.
        /// </summary>
        /// <param name="proxyDefinition">The proxy definition.</param>
        /// <param name="implementationType">The implementation type.</param>
        /// <param name="eventInfos">The event informations.</param>
        /// <param name="propertyInfos">The property informations.</param>
        /// <param name="methodInfos">The method informations.</param>
        public ProxyTemplate(IProxyDefinition proxyDefinition, Type implementationType, ICollection<EventInfo> eventInfos, ICollection<PropertyInfo> propertyInfos, ICollection<MethodInfo> methodInfos)
        {
            if (proxyDefinition == null)
                throw new ArgumentNullException("proxyDefinition");

            if (implementationType == null)
                throw new ArgumentNullException("implementationType");

            if (eventInfos == null)
                throw new ArgumentNullException("eventInfos");

            if (propertyInfos == null)
                throw new ArgumentNullException("propertyInfos");

            if (methodInfos == null)
                throw new ArgumentNullException("methodInfos");

            _proxyDefinition = proxyDefinition;
            _implementationType = implementationType;
            _eventInfos = eventInfos;
            _propertyInfos = propertyInfos;
            _methodInfos = methodInfos;
        }
示例#8
0
        /// <summary>
        /// Generates a proxy template.
        /// </summary>
        /// <param name="proxyDefinition">The proxy definition.</param>
        /// <returns>The proxy template.</returns>
        private IProxyTemplate GenerateProxyTemplate(IProxyDefinition proxyDefinition)
        {
            var typeBuilder = _typeBuilderFactory.CreateBuilder(proxyDefinition.ParentType);
            var proxyGenerator = new ProxyGenerator(typeBuilder, _interceptionFilter);

            return proxyGenerator.GenerateProxyTemplate(proxyDefinition);
        }