コード例 #1
0
        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);
        }
コード例 #2
0
        public Type CreateProxyType(Type t)
        {
            Guard.ArgumentNotNull(t, "t");
            if (!CanIntercept(t))
            {
                throw new InvalidOperationException(Resources.InterceptionNotSupported);
            }
            InterceptingClassGenerator classGenerator = new InterceptingClassGenerator(t);

            return(classGenerator.GenerateType());
        }
コード例 #3
0
        public void CanInterceptClassWithReservedTypeAttributes()
        {
            InterceptingClassGenerator generator =
                new InterceptingClassGenerator(typeof(HtmlInputText));

            Type generatedType = generator.GenerateType();

            HtmlInputText instance = (HtmlInputText)Activator.CreateInstance(generatedType);

            bool intercepted = false;

            ((IInterceptingProxy)instance)
                .AddInterceptionBehavior(
                    new ActionInterceptionBehavior(() => intercepted = true));

            var result = instance.HasControls();

            Assert.IsTrue(intercepted);
        }
コード例 #4
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);
        }
コード例 #5
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;
        }