Exemplo n.º 1
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));
        }
        protected virtual void ImplementCacheInvocationCache()
        {
            MethodInfo method = typeof(HybridDictionary).GetMethod("get_Item", new Type[] { typeof(object) });
            MethodInfo info2  = typeof(HybridDictionary).GetMethod("Add", new Type[] { typeof(object), typeof(object) });

            Type[]            types      = new Type[] { typeof(ICallable), typeof(object), typeof(MethodInfo), typeof(object) };
            ArgumentReference reference  = new ArgumentReference(typeof(ICallable));
            ArgumentReference reference2 = new ArgumentReference(typeof(MethodInfo));
            ArgumentReference reference3 = new ArgumentReference(typeof(object));

            this._method2Invocation = this.MainTypeBuilder.CreateMethod("_Method2Invocation", new ReturnReferenceExpression(this.Context.Invocation), MethodAttributes.HideBySig | MethodAttributes.Family, new ArgumentReference[] { reference, reference2, reference3 });
            LocalReference      target     = this._method2Invocation.CodeBuilder.DeclareLocal(this.Context.Invocation);
            LockBlockExpression expression = new LockBlockExpression(SelfReference.Self);

            expression.AddStatement(new AssignStatement(target, new ConvertExpression(this.Context.Invocation, new VirtualMethodInvocationExpression(this.CacheField, method, new Expression[] { reference2.ToExpression() }))));
            ConditionExpression expression2 = new ConditionExpression(OpCodes.Brfalse_S, target.ToExpression());

            expression2.AddTrueStatement(new AssignStatement(target, new NewInstanceExpression(this.InvocationType.GetConstructor(types), new Expression[] { reference.ToExpression(), SelfReference.Self.ToExpression(), reference2.ToExpression(), reference3.ToExpression() })));
            expression2.AddTrueStatement(new ExpressionStatement(new VirtualMethodInvocationExpression(this.CacheField, info2, new Expression[] { reference2.ToExpression(), target.ToExpression() })));
            expression.AddStatement(new ExpressionStatement(expression2));
            this._method2Invocation.CodeBuilder.AddStatement(new ExpressionStatement(expression));
            this._method2Invocation.CodeBuilder.AddStatement(new ReturnStatement(target));
        }
        public void Conditionals()
        {
            EasyType typebuilder = new EasyType(module, "mytype");

            FieldReference cachefield = typebuilder.CreateField("cache", typeof(IDictionary));

            ArgumentReference arg = new ArgumentReference(typeof(bool));

            EasyConstructor constructor = typebuilder.CreateConstructor(arg);

            constructor.CodeBuilder.InvokeBaseConstructor();

            ConditionExpression exp = new ConditionExpression(OpCodes.Brtrue_S, arg.ToExpression());

            exp.AddTrueStatement(new AssignStatement(cachefield,
                                                     new NewInstanceExpression(typeof(HybridDictionary), new Type[0])));
            exp.AddFalseStatement(new AssignStatement(cachefield,
                                                      new NewInstanceExpression(typeof(Hashtable), new Type[0])));

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

            ReturnReferenceExpression ret = new ReturnReferenceExpression(typeof(IDictionary));
            EasyMethod getCache           = typebuilder.CreateMethod("GetCache", ret);

            getCache.CodeBuilder.AddStatement(new ReturnStatement(cachefield));

            Type       newType  = typebuilder.BuildType();
            object     instance = Activator.CreateInstance(newType, new object[] { true });
            MethodInfo method   = instance.GetType().GetMethod("GetCache");
            object     dic      = method.Invoke(instance, new object[0]);

            Assert.IsTrue(dic is HybridDictionary);

            instance = Activator.CreateInstance(newType, new object[] { false });
            dic      = method.Invoke(instance, new object[0]);
            Assert.IsTrue(dic is Hashtable);

            RunPEVerify();
        }