Exemplo n.º 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 Tuple <Type, Func <object> > CreateProxyType(Type t, bool compileProxyContructor = true)
        {
            Type                         interceptorType;
            Type                         typeToDerive = t;
            bool                         genericType  = false;
            GeneratedTypeKey             key          = new GeneratedTypeKey(typeToDerive);
            Tuple <Type, Func <object> > tuple;

            if (DerivedClasses.TryGetValue(key, out tuple))
            {
                return(tuple);
            }
            if (t.IsGenericType)
            {
                typeToDerive = t.GetGenericTypeDefinition();
                genericType  = true;
            }
            if (!CanIntercept(t))
            {
                throw new InvalidOperationException(
                          string.Format(CultureInfo.CurrentCulture, Resources.InterceptionNotSupported, t.Name));
            }
            lock (DerivedClasses)
            {
                InterceptingClassGenerator generator = new InterceptingClassGenerator(typeToDerive);
                interceptorType = generator.GenerateType();
                if (genericType)
                {
                    interceptorType = interceptorType.MakeGenericType(t.GetGenericArguments());
                }
                Func <object> contructor = null;
                if (compileProxyContructor)
                {
                    contructor = Expression.Lambda <Func <object> >(Expression.New(interceptorType)).Compile();
                }
                tuple = new Tuple <Type, Func <object> >(interceptorType, contructor);
                DerivedClasses[key] = tuple;
            }
            if (genericType)
            {
                IList <PropertyInfoEx> tableInfo;
                TypePropertiesCache.VerifyInformationIsLoaded(interceptorType, out tableInfo);
            }
            else
            {
                TypePropertiesCache.AssociateTypeWithProxyType(t, interceptorType);
            }
            PropertiesExDictionary dic;

            TypePropertiesCache.cachePropertiesEx.TryGetValue(interceptorType, out dic);
            interceptorType.GetField("__Props").SetValue(null, (PropertyInfoEx[])dic.PropertiesList);
            //PropertyInfoEx[] currentProps;
            //TypePropertiesCache.cachePropertiesEx.TryGetValue(t, out currentProps);
            //TypePropertiesCache.cachePropertiesEx.Remove(t);
            //TypePropertiesCache.cachePropertiesEx.Add(interceptorType, currentProps);
            return(tuple);
        }
Exemplo n.º 3
0
        public Type CreateProxyType(Type t, params Type[] additionalInterfaces)
        {
            Guard.ArgumentNotNull(t, "t");
            Guard.ArgumentNotNull(additionalInterfaces, "additionalInterfaces");

            if (!CanIntercept(t))
            {
                throw new InvalidOperationException(
                          string.Format(CultureInfo.CurrentCulture, Resources.InterceptionNotSupported, t.Name));
            }

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

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

            GeneratedTypeKey key = new GeneratedTypeKey(typeToDerive, additionalInterfaces);

            lock (DerivedClasses)
            {
                if (!DerivedClasses.TryGetValue(key, out interceptorType))
                {
                    InterceptingClassGenerator generator =
                        new InterceptingClassGenerator(typeToDerive, additionalInterfaces);
                    interceptorType     = generator.GenerateType();
                    DerivedClasses[key] = interceptorType;
                }
            }

            if (genericType)
            {
                interceptorType = interceptorType.MakeGenericType(t.GetGenericArguments());
            }

            return(interceptorType);
        }