Ejemplo n.º 1
0
        /// <summary>
        /// Generates one public constructor receiving
        /// the <see cref="IInterceptor"/> instance and instantiating a hashtable
        /// </summary>
        protected virtual EasyConstructor GenerateConstructor(ConstructorInfo baseConstructor)
        {
            ArrayList arguments = new ArrayList();

            ArgumentReference arg1 = new ArgumentReference(Context.Interceptor);
            ArgumentReference arg2 = new ArgumentReference(typeof(object[]));

            arguments.Add(arg1);

            ParameterInfo[] parameters = baseConstructor.GetParameters();

            if (Context.HasMixins)
            {
                arguments.Add(arg2);
            }

            ArgumentReference[] originalArguments =
                ArgumentsUtil.ConvertToArgumentReference(parameters);

            arguments.AddRange(originalArguments);

            EasyConstructor constructor = MainTypeBuilder.CreateConstructor(
                (ArgumentReference[])arguments.ToArray(typeof(ArgumentReference)));

            GenerateConstructorCode(constructor.CodeBuilder, arg1, SelfReference.Self, arg2);

            constructor.CodeBuilder.InvokeBaseConstructor(baseConstructor, originalArguments);

            constructor.CodeBuilder.AddStatement(new ReturnStatement());

            return(constructor);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generates one public constructor receiving
        /// the <see cref="IInterceptor"/> instance and instantiating a HybridCollection
        /// </summary>
        protected override EasyConstructor GenerateConstructor()
        {
            ArgumentReference arg1 = new ArgumentReference(Context.Interceptor);
            ArgumentReference arg2 = new ArgumentReference(typeof(object));
            ArgumentReference arg3 = new ArgumentReference(typeof(object[]));

            EasyConstructor constructor;

            if (Context.HasMixins)
            {
                constructor = MainTypeBuilder.CreateConstructor(arg1, arg2, arg3);
            }
            else
            {
                constructor = MainTypeBuilder.CreateConstructor(arg1, arg2);
            }

            GenerateConstructorCode(constructor.CodeBuilder, arg1, SelfReference.Self, arg3);

            constructor.CodeBuilder.InvokeBaseConstructor();

            constructor.CodeBuilder.AddStatement(new AssignStatement(
                                                     _targetField, arg2.ToExpression()));

            constructor.CodeBuilder.AddStatement(new ReturnStatement());

            return(constructor);
        }
Ejemplo n.º 3
0
        protected virtual Type CreateType()
        {
            Type newType = MainTypeBuilder.BuildType();

            RegisterInCache(newType);

            return(newType);
        }
Ejemplo n.º 4
0
        private EasyProperty CreateInterceptorProperty(PropertyInfo propertyInfo)
        {
            EasyProperty interceptorProperty = MainTypeBuilder.CreateProperty(propertyInfo);
            EasyMethod   getMethod           = interceptorProperty.CreateGetMethod();
            MethodInfo   baseMethod          = typeof(MulticastDelegate).GetMethod("get_Interceptor");

            getMethod.CodeBuilder.AddStatement(new ReturnStatement(base.InterceptorField));
            return(interceptorProperty);
        }
Ejemplo n.º 5
0
        protected virtual void PreProcessMethod(MethodInfo method)
        {
            MethodInfo callbackMethod = GenerateCallbackMethodIfNecessary(method, null);

            EasyCallable callable = MainTypeBuilder.CreateCallable(method.ReturnType, method.GetParameters());

            _method2Delegate[method] = callable;

            FieldReference field = MainTypeBuilder.CreateField(
                String.Format("_cached_{0}", callable.ID),
                callable.TypeBuilder);

            RegisterDelegateFieldToBeInitialized(method, field, callable, callbackMethod);
        }
Ejemplo n.º 6
0
        protected virtual void ImplementCacheInvocationCache()
        {
            MethodInfo get_ItemMethod = typeof(HybridDictionary).GetMethod("get_Item", new Type[] { typeof(object) });
            MethodInfo set_ItemMethod = typeof(HybridDictionary).GetMethod("Add", new Type[] { typeof(object), typeof(object) });

            Type[] args = new Type[] { typeof(ICallable), typeof(MethodInfo) };
            Type[] invocation_const_args = new Type[] { typeof(ICallable), typeof(object), typeof(MethodInfo), typeof(object) };

            ArgumentReference arg1 = new ArgumentReference(typeof(ICallable));
            ArgumentReference arg2 = new ArgumentReference(typeof(MethodInfo));
            ArgumentReference arg3 = new ArgumentReference(typeof(object));

            _method2Invocation = MainTypeBuilder.CreateMethod("_Method2Invocation",
                                                              new ReturnReferenceExpression(Context.Invocation),
                                                              MethodAttributes.Family | MethodAttributes.HideBySig, arg1, arg2,
                                                              arg3);

            LocalReference invocation_local =
                _method2Invocation.CodeBuilder.DeclareLocal(Context.Invocation);

            LockBlockExpression block = new LockBlockExpression(SelfReference.Self);

            block.AddStatement(new AssignStatement(invocation_local,
                                                   new ConvertExpression(Context.Invocation,
                                                                         new VirtualMethodInvocationExpression(CacheField,
                                                                                                               get_ItemMethod,
                                                                                                               arg2.ToExpression()))));

            ConditionExpression cond1 = new ConditionExpression(OpCodes.Brfalse_S,
                                                                invocation_local.ToExpression());

            cond1.AddTrueStatement(new AssignStatement(
                                       invocation_local,
                                       new NewInstanceExpression(InvocationType.GetConstructor(invocation_const_args),
                                                                 arg1.ToExpression(), SelfReference.Self.ToExpression(),
                                                                 arg2.ToExpression(), arg3.ToExpression())));

            cond1.AddTrueStatement(new ExpressionStatement(
                                       new VirtualMethodInvocationExpression(CacheField,
                                                                             set_ItemMethod, arg2.ToExpression(),
                                                                             invocation_local.ToExpression())));

            block.AddStatement(new ExpressionStatement(cond1));

            _method2Invocation.CodeBuilder.AddStatement(new ExpressionStatement(block));
            _method2Invocation.CodeBuilder.AddStatement(new ReturnStatement(invocation_local));
        }
Ejemplo n.º 7
0
        protected virtual MethodInfo GenerateCallbackMethodIfNecessary(MethodInfo method, Reference invocationTarget)
        {
            if (Context.HasMixins && _interface2mixinIndex.Contains(method.DeclaringType))
            {
                return(method);
            }

            String name = String.Format("callback__{0}", method.Name);

            ParameterInfo[] parameters = method.GetParameters();

            ArgumentReference[] args = new ArgumentReference[parameters.Length];

            for (int i = 0; i < args.Length; i++)
            {
                args[i] = new ArgumentReference(parameters[i].ParameterType);
            }

            EasyMethod easymethod = MainTypeBuilder.CreateMethod(name,
                                                                 new ReturnReferenceExpression(method.ReturnType),
                                                                 MethodAttributes.HideBySig | MethodAttributes.Public, args);

            Expression[] exps = new Expression[parameters.Length];

            for (int i = 0; i < args.Length; i++)
            {
                exps[i] = args[i].ToExpression();
            }

            if (invocationTarget == null)
            {
                easymethod.CodeBuilder.AddStatement(
                    new ReturnStatement(
                        new MethodInvocationExpression(method, exps)));
            }
            else
            {
                easymethod.CodeBuilder.AddStatement(
                    new ReturnStatement(
                        new MethodInvocationExpression(invocationTarget, method, exps)));
            }

            return(easymethod.MethodBuilder);
        }
Ejemplo n.º 8
0
        protected void GenerateSerializationConstructor()
        {
            ArgumentReference arg1 = new ArgumentReference(typeof(SerializationInfo));
            ArgumentReference arg2 = new ArgumentReference(typeof(StreamingContext));

            EasyConstructor constr = MainTypeBuilder.CreateConstructor(arg1, arg2);

            constr.CodeBuilder.AddStatement(new ExpressionStatement(
                                                new ConstructorInvocationExpression(_serializationConstructor,
                                                                                    arg1.ToExpression(), arg2.ToExpression())));

            Type[]     object_arg     = new Type[] { typeof(String), typeof(Type) };
            MethodInfo getValueMethod = typeof(SerializationInfo).GetMethod("GetValue", object_arg);

            VirtualMethodInvocationExpression getInterceptorInvocation =
                new VirtualMethodInvocationExpression(arg1, getValueMethod,
                                                      new FixedReference("__interceptor").ToExpression(),
                                                      new TypeTokenExpression(Context.Interceptor));

            VirtualMethodInvocationExpression getMixinsInvocation =
                new VirtualMethodInvocationExpression(arg1, getValueMethod,
                                                      new FixedReference("__mixins").ToExpression(),
                                                      new TypeTokenExpression(typeof(object[])));

            constr.CodeBuilder.AddStatement(new AssignStatement(
                                                InterceptorField, getInterceptorInvocation));

            constr.CodeBuilder.AddStatement(new AssignStatement(
                                                CacheField, new NewInstanceExpression(
                                                    typeof(HybridDictionary).GetConstructor(new Type[0]))));

            constr.CodeBuilder.AddStatement(new AssignStatement(
                                                MixinField,
                                                getMixinsInvocation));

            // Initialize the delegate fields
            foreach (CallableField field in _cachedFields)
            {
                field.WriteInitialization(constr.CodeBuilder, SelfReference.Self, MixinField);
            }

            constr.CodeBuilder.AddStatement(new ReturnStatement());
        }
Ejemplo n.º 9
0
        protected virtual void ImplementGetObjectData(Type[] interfaces)
        {
            // To prevent re-implementation of this interface.
            _generated.Add(typeof(ISerializable));

            Type[]     get_type_args  = new Type[] { typeof(String), typeof(bool), typeof(bool) };
            Type[]     key_and_object = new Type[] { typeof(String), typeof(Object) };
            MethodInfo addValueMethod = typeof(SerializationInfo).GetMethod("AddValue", key_and_object);

            ArgumentReference arg1          = new ArgumentReference(typeof(SerializationInfo));
            ArgumentReference arg2          = new ArgumentReference(typeof(StreamingContext));
            EasyMethod        getObjectData = MainTypeBuilder.CreateMethod("GetObjectData",
                                                                           new ReturnReferenceExpression(typeof(void)), arg1, arg2);

            LocalReference typeLocal = getObjectData.CodeBuilder.DeclareLocal(typeof(Type));

            getObjectData.CodeBuilder.AddStatement(new AssignStatement(
                                                       typeLocal,
                                                       new MethodInvocationExpression(null,
                                                                                      typeof(Type).GetMethod("GetType",
                                                                                                             get_type_args),
                                                                                      new FixedReference(
                                                                                          Context.ProxyObjectReference.
                                                                                          AssemblyQualifiedName).ToExpression(),
                                                                                      new FixedReference(1).ToExpression(),
                                                                                      new FixedReference(0).ToExpression())));

            getObjectData.CodeBuilder.AddStatement(new ExpressionStatement(
                                                       new VirtualMethodInvocationExpression(
                                                           arg1, typeof(SerializationInfo).GetMethod("SetType"),
                                                           typeLocal.ToExpression())));

            getObjectData.CodeBuilder.AddStatement(new ExpressionStatement(
                                                       new VirtualMethodInvocationExpression(arg1, addValueMethod,
                                                                                             new FixedReference("__interceptor").
                                                                                             ToExpression(),
                                                                                             InterceptorField.ToExpression())));

            getObjectData.CodeBuilder.AddStatement(new ExpressionStatement(
                                                       new VirtualMethodInvocationExpression(arg1, addValueMethod,
                                                                                             new FixedReference("__mixins").
                                                                                             ToExpression(),
                                                                                             MixinField.ToExpression())));

            LocalReference interfacesLocal =
                getObjectData.CodeBuilder.DeclareLocal(typeof(String[]));

            getObjectData.CodeBuilder.AddStatement(
                new AssignStatement(interfacesLocal,
                                    new NewArrayExpression(interfaces.Length, typeof(String))));

            for (int i = 0; i < interfaces.Length; i++)
            {
                getObjectData.CodeBuilder.AddStatement(new AssignArrayStatement(
                                                           interfacesLocal, i,
                                                           new FixedReference(interfaces[i].AssemblyQualifiedName).ToExpression()));
            }

            getObjectData.CodeBuilder.AddStatement(new ExpressionStatement(
                                                       new VirtualMethodInvocationExpression(arg1, addValueMethod,
                                                                                             new FixedReference("__interfaces").
                                                                                             ToExpression(),
                                                                                             interfacesLocal.ToExpression())));

            getObjectData.CodeBuilder.AddStatement(new ExpressionStatement(
                                                       new VirtualMethodInvocationExpression(arg1, addValueMethod,
                                                                                             new FixedReference("__baseType").
                                                                                             ToExpression(),
                                                                                             new TypeTokenExpression(_baseType))));

            CustomizeGetObjectData(getObjectData.CodeBuilder, arg1, arg2);

            getObjectData.CodeBuilder.AddStatement(new ReturnStatement());
        }
Ejemplo n.º 10
0
 protected override void GenerateFields()
 {
     base.GenerateFields();
     _targetField = MainTypeBuilder.CreateField("__target", typeof(object));
 }