Exemple #1
0
        public IInterceptingProxy CreateProxy(Type t, object target, params Type[] additionalInterfaces)
        {
            Guard.ArgumentNotNull(t, "t");
            Guard.ArgumentNotNull(additionalInterfaces, "additionalInterfaces");

            Type interceptorType;
            Type typeToProxy = t;
            bool genericType = false;

            if (t.IsGenericType())
            {
                typeToProxy = t.GetGenericTypeDefinition();
                genericType = true;
            }

            GeneratedTypeKey key = new GeneratedTypeKey(typeToProxy, additionalInterfaces);

            lock (InterceptorClasses)
            {
                if (!InterceptorClasses.TryGetValue(key, out interceptorType))
                {
                    InterfaceInterceptorClassGenerator generator =
                        new InterfaceInterceptorClassGenerator(typeToProxy, additionalInterfaces);
                    interceptorType         = generator.CreateProxyType();
                    InterceptorClasses[key] = interceptorType;
                }
            }

            if (genericType)
            {
                interceptorType = interceptorType.MakeGenericType(t.GetGenericArguments());
            }
            return((IInterceptingProxy)interceptorType.GetConstructors()[0].Invoke(new object[] { target, t }));
        }
        public IInterceptingProxy CreateProxy(Type t, object target, params Type[] additionalInterfaces)
        {
            Guard.ArgumentNotNull(t, "t");
            Guard.ArgumentNotNull(additionalInterfaces, "additionalInterfaces");

            Type interceptorType;
            Type typeToProxy = t;
            bool genericType = false;

            if (t.IsGenericType)
            {
                typeToProxy = t.GetGenericTypeDefinition();
                genericType = true;
            }

            GeneratedTypeKey key = new GeneratedTypeKey(typeToProxy, additionalInterfaces);
            lock (InterceptorClasses)
            {
                if (!InterceptorClasses.TryGetValue(key, out interceptorType))
                {
                    InterfaceInterceptorClassGenerator generator =
                        new InterfaceInterceptorClassGenerator(typeToProxy, additionalInterfaces);
                    interceptorType = generator.CreateProxyType();
                    InterceptorClasses[key] = interceptorType;
                }
            }

            if (genericType)
            {
                interceptorType = interceptorType.MakeGenericType(t.GetGenericArguments());
            }
            return (IInterceptingProxy)interceptorType.GetConstructors()[0].Invoke(new object[] { target, t });
        }
Exemple #3
0
        /// <summary>
        /// Create a proxy object that provides interception for <paramref name="target"/>.
        /// </summary>
        /// <param name="t">Type to generate the proxy of.</param>
        /// <param name="target">Object to create the proxy for.</param>
        /// <returns>The proxy object.</returns>
        public IInterceptingProxy CreateProxy(Type t, object target)
        {
            Type interceptorType;
            Type typeToProxy = t;

            if (t.IsGenericType)
            {
                typeToProxy = t.GetGenericTypeDefinition();
            }
            lock (interceptorClasses)
            {
                if (!interceptorClasses.ContainsKey(typeToProxy))
                {
                    InterfaceInterceptorClassGenerator generator = new InterfaceInterceptorClassGenerator(typeToProxy);
                    interceptorClasses[typeToProxy] = generator.CreateProxyType();
                }
                interceptorType = interceptorClasses[typeToProxy];
            }
            if (interceptorType.IsGenericTypeDefinition)
            {
                interceptorType = interceptorType.MakeGenericType(t.GetGenericArguments());
            }
            return((IInterceptingProxy)Activator.CreateInstance(interceptorType, target));
        }