public void NewInstanceExpression() { EasyType typebuilder = new EasyType(module, "mytype"); FieldReference cachefield = typebuilder.CreateField("cache", typeof(ArrayList)); EasyConstructor constructor = typebuilder.CreateConstructor( ); constructor.CodeBuilder.InvokeBaseConstructor(); constructor.CodeBuilder.AddStatement(new AssignStatement(cachefield, new NewInstanceExpression(typeof(ArrayList), new Type[0]))); constructor.CodeBuilder.AddStatement(new ReturnStatement()); ReturnReferenceExpression ret = new ReturnReferenceExpression(typeof(ArrayList)); EasyMethod getCache = typebuilder.CreateMethod("GetCache", ret); getCache.CodeBuilder.AddStatement(new ReturnStatement(cachefield)); Type newType = typebuilder.BuildType(); Assert.IsNotNull(newType); object instance = Activator.CreateInstance(newType, new object[0]); Assert.IsNotNull(instance); MethodInfo method = instance.GetType().GetMethod("GetCache"); Assert.IsNotNull(method.Invoke(instance, new object[0])); RunPEVerify(); }
/// <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); }
public void CreateMoreComplexCallable() { EasyType typebuilder = new EasyType(module, "mytype"); ArgumentReference arg1 = new ArgumentReference(typeof(int)); ArgumentReference arg2 = new ArgumentReference(typeof(DateTime)); ArgumentReference arg3 = new ArgumentReference(typeof(object)); EasyCallable callable = typebuilder.CreateCallable( new ReturnReferenceExpression(typeof(string)), arg1, arg2, arg3); FieldReference field1 = typebuilder.CreateField("field1", callable.TypeBuilder); SimpleCallback sc = new SimpleCallback(); ArgumentReference arg = new ArgumentReference(typeof(SimpleCallback)); EasyConstructor constructor = typebuilder.CreateConstructor(arg); constructor.CodeBuilder.InvokeBaseConstructor(); constructor.CodeBuilder.AddStatement(new AssignStatement(field1, new NewInstanceExpression(callable, arg.ToExpression(), new MethodPointerExpression(arg, typeof(SimpleCallback).GetMethod("RunAs"))))); constructor.CodeBuilder.AddStatement(new ReturnStatement()); arg1 = new ArgumentReference(typeof(int)); arg2 = new ArgumentReference(typeof(DateTime)); arg3 = new ArgumentReference(typeof(object)); ReturnReferenceExpression ret1 = new ReturnReferenceExpression(typeof(string)); EasyMethod getField1 = typebuilder.CreateMethod("Exec", ret1, arg1, arg2, arg3); getField1.CodeBuilder.AddStatement( new ReturnStatement( new ConvertExpression(typeof(String), new MethodInvocationExpression(field1, callable.Callmethod, new ReferencesToObjectArrayExpression(arg1, arg2, arg3))))); Type newType = typebuilder.BuildType(); RunPEVerify(); object instance = Activator.CreateInstance(newType, new object[] { sc }); MethodInfo method = instance.GetType().GetMethod("Exec"); object result = method.Invoke(instance, new object[] { 1, DateTime.Now, "" }); Assert.AreEqual("hello2", result); }
public void ArrayRefs() { EasyType typebuilder = new EasyType(module, "mytype"); FieldReference field1 = typebuilder.CreateField("field1", typeof(object)); FieldReference field2 = typebuilder.CreateField("field2", typeof(object)); ArgumentReference arg = new ArgumentReference(typeof(object[])); EasyConstructor constructor = typebuilder.CreateConstructor(arg); constructor.CodeBuilder.InvokeBaseConstructor(); constructor.CodeBuilder.AddStatement(new AssignStatement(field1, new LoadRefArrayElementExpression(0, arg))); constructor.CodeBuilder.AddStatement(new AssignStatement(field2, new LoadRefArrayElementExpression(1, arg))); constructor.CodeBuilder.AddStatement(new ReturnStatement()); ReturnReferenceExpression ret1 = new ReturnReferenceExpression(typeof(object)); EasyMethod getField1 = typebuilder.CreateMethod("GetField1", ret1); getField1.CodeBuilder.AddStatement(new ReturnStatement(field1)); ReturnReferenceExpression ret2 = new ReturnReferenceExpression(typeof(object)); EasyMethod getField2 = typebuilder.CreateMethod("GetField2", ret2); getField2.CodeBuilder.AddStatement(new ReturnStatement(field2)); Type newType = typebuilder.BuildType(); object[] innerArgs = new object[] { "hammett", "verissimo" }; object instance = Activator.CreateInstance(newType, new object[] { innerArgs }); MethodInfo method = instance.GetType().GetMethod("GetField1"); object result = method.Invoke(instance, new object[0]); Assert.AreEqual("hammett", result); method = instance.GetType().GetMethod("GetField2"); result = method.Invoke(instance, new object[0]); Assert.AreEqual("verissimo", result); RunPEVerify(); }
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()); }
public void FieldsStoreAndLoad() { EasyType typebuilder = new EasyType(module, "mytype"); FieldReference field1 = typebuilder.CreateField("field1", typeof(int)); FieldReference field2 = typebuilder.CreateField("field2", typeof(string)); { ArgumentReference arg1 = new ArgumentReference(typeof(int)); ArgumentReference arg2 = new ArgumentReference(typeof(string)); EasyConstructor constr = typebuilder.CreateConstructor(arg1, arg2); constr.CodeBuilder.InvokeBaseConstructor(); constr.CodeBuilder.AddStatement(new AssignStatement(field1, arg1.ToExpression())); constr.CodeBuilder.AddStatement(new AssignStatement(field2, arg2.ToExpression())); constr.CodeBuilder.AddStatement(new ReturnStatement()); } { ReturnReferenceExpression ret1 = new ReturnReferenceExpression(typeof(int)); EasyMethod m1 = typebuilder.CreateMethod("GetField1", ret1); m1.CodeBuilder.AddStatement(new ReturnStatement(field1)); ReturnReferenceExpression ret2 = new ReturnReferenceExpression(typeof(string)); EasyMethod m2 = typebuilder.CreateMethod("GetField2", ret2); m2.CodeBuilder.AddStatement(new ReturnStatement(field2)); } Type newType = typebuilder.BuildType(); Assert.IsNotNull(newType); object instance = Activator.CreateInstance(newType, new object[] { 10, "hello" }); Assert.IsNotNull(instance); MethodInfo method1 = instance.GetType().GetMethod("GetField1"); MethodInfo method2 = instance.GetType().GetMethod("GetField2"); Assert.AreEqual(10, method1.Invoke(instance, new object[0])); Assert.AreEqual("hello", method2.Invoke(instance, new object[0])); RunPEVerify(); }
protected void GenerateSerializationConstructor() { ArgumentReference owner = new ArgumentReference(typeof(SerializationInfo)); ArgumentReference reference2 = new ArgumentReference(typeof(StreamingContext)); EasyConstructor constructor = base.MainTypeBuilder.CreateConstructor(new ArgumentReference[] { owner, reference2 }); constructor.CodeBuilder.AddStatement(new ExpressionStatement(new ConstructorInvocationExpression(this._serializationConstructor, new Expression[] { owner.ToExpression(), reference2.ToExpression() }))); Type[] types = new Type[] { typeof(string), typeof(Type) }; MethodInfo method = typeof(SerializationInfo).GetMethod("GetValue", types); VirtualMethodInvocationExpression expression = new VirtualMethodInvocationExpression(owner, method, new Expression[] { new FixedReference("__interceptor").ToExpression(), new TypeTokenExpression(base.Context.Interceptor) }); VirtualMethodInvocationExpression expression2 = new VirtualMethodInvocationExpression(owner, method, new Expression[] { new FixedReference("__mixins").ToExpression(), new TypeTokenExpression(typeof(object[])) }); constructor.CodeBuilder.AddStatement(new AssignStatement(base.InterceptorField, expression)); constructor.CodeBuilder.AddStatement(new AssignStatement(base.CacheField, new NewInstanceExpression(typeof(HybridDictionary).GetConstructor(new Type[0]), new Expression[0]))); constructor.CodeBuilder.AddStatement(new AssignStatement(base.MixinField, expression2)); foreach (CallableField field in base._cachedFields) { field.WriteInitialization(constructor.CodeBuilder, SelfReference.Self, base.MixinField); } constructor.CodeBuilder.AddStatement(new ReturnStatement()); }
protected virtual EasyConstructor GenerateConstructor(ConstructorInfo baseConstructor) { ArrayList list = new ArrayList(); ArgumentReference reference = new ArgumentReference(base.Context.Interceptor); ArgumentReference reference2 = new ArgumentReference(typeof(object[])); list.Add(reference); ParameterInfo[] parameters = baseConstructor.GetParameters(); if (base.Context.HasMixins) { list.Add(reference2); } ArgumentReference[] c = ArgumentsUtil.ConvertToArgumentReference(parameters); list.AddRange(c); EasyConstructor constructor = base.MainTypeBuilder.CreateConstructor((ArgumentReference[])list.ToArray(typeof(ArgumentReference))); this.GenerateConstructorCode(constructor.CodeBuilder, reference, SelfReference.Self, reference2); constructor.CodeBuilder.InvokeBaseConstructor(baseConstructor, c); constructor.CodeBuilder.AddStatement(new ReturnStatement()); return(constructor); }
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(); }