Ejemplo n.º 1
0
        public void GotoLabel()
        {
            var provider = new ReflectionProvider();
            var assemblyBuilder = new EmitAssemblyBuilder("TestAssembly");
            var typeBuilder = assemblyBuilder.DefineType("", "TestClass");
            var method = typeBuilder.DefineMethod("Foo", typeof(string));
            var variable = method.Il.DeclareLocal(typeof(int));
            var label = method.Il.DefineLabel();
            method.Il.Emit(Ldc_I4_0);
            method.Il.Emit(Stloc, variable);
            method.Il.MarkLabel(label);
            method.Il.Emit(Ldloc, variable);
            method.Il.Emit(Ldc_I4_1);
            method.Il.Emit(Add);
            method.Il.Emit(Dup);
            method.Il.Emit(Stloc, variable);
            method.Il.Emit(Ldc_I4_5);
            method.Il.Emit(Blt, label);
            method.Il.Emit(Ldloc, variable);
            method.Il.Emit(Ret);
            var type = provider.Compile(assemblyBuilder).GetType("TestClass");

            var instance = Activator.CreateInstance(type);
            var methodInfo = type.GetMethod("Foo");

            var result = (int)methodInfo.Invoke(instance, null);
            Assert.AreEqual(5, result);            
        }
Ejemplo n.º 2
0
        public void GotoLabel()
        {
            var provider        = new ReflectionProvider();
            var assemblyBuilder = new EmitAssemblyBuilder("TestAssembly");
            var typeBuilder     = assemblyBuilder.DefineType("", "TestClass");
            var method          = typeBuilder.DefineMethod("Foo", typeof(string));
            var variable        = method.Il.DeclareLocal(typeof(int));
            var label           = method.Il.DefineLabel();

            method.Il.Emit(Ldc_I4_0);
            method.Il.Emit(Stloc, variable);
            method.Il.MarkLabel(label);
            method.Il.Emit(Ldloc, variable);
            method.Il.Emit(Ldc_I4_1);
            method.Il.Emit(Add);
            method.Il.Emit(Dup);
            method.Il.Emit(Stloc, variable);
            method.Il.Emit(Ldc_I4_5);
            method.Il.Emit(Blt, label);
            method.Il.Emit(Ldloc, variable);
            method.Il.Emit(Ret);
            var type = provider.Compile(assemblyBuilder).GetType("TestClass");

            var instance   = Activator.CreateInstance(type);
            var methodInfo = type.GetMethod("Foo");

            var result = (int)methodInfo.Invoke(instance, null);

            Assert.AreEqual(5, result);
        }
Ejemplo n.º 3
0
        public void ClassExtendsBaseType()
        {
            var provider = new ReflectionProvider();
            var assemblyBuilder = new EmitAssemblyBuilder("TestAssembly");
            var typeBuilder = assemblyBuilder.DefineType("", "TestClass", baseType: typeof(CustomBaseType));
            var method = typeBuilder.DefineMethod("Foo", typeof(void), EmitVisibility.Public, isStatic: true);
            method.Il.Emit(EmitOpCodes.Ret);
            var type = provider.Compile(assemblyBuilder).GetType("TestClass");

            Assert.IsTrue(typeof(CustomBaseType).IsAssignableFrom(type));
        }
Ejemplo n.º 4
0
        public void ClassExtendsBaseType()
        {
            var provider        = new ReflectionProvider();
            var assemblyBuilder = new EmitAssemblyBuilder("TestAssembly");
            var typeBuilder     = assemblyBuilder.DefineType("", "TestClass", baseType: typeof(CustomBaseType));
            var method          = typeBuilder.DefineMethod("Foo", typeof(void), EmitVisibility.Public, isStatic: true);

            method.Il.Emit(EmitOpCodes.Ret);
            var type = provider.Compile(assemblyBuilder).GetType("TestClass");

            Assert.IsTrue(typeof(CustomBaseType).IsAssignableFrom(type));
        }
Ejemplo n.º 5
0
        public void ProtectedMethodIsProtected()
        {
            var provider = new ReflectionProvider();
            var assemblyBuilder = new EmitAssemblyBuilder("TestAssembly");
            var typeBuilder = assemblyBuilder.DefineType("", "TestClass");
            var method = typeBuilder.DefineMethod("Foo", typeof(void), EmitVisibility.Protected);
            method.Il.Emit(EmitOpCodes.Ret);
            var type = provider.Compile(assemblyBuilder).GetType("TestClass");

            var methodInfo = type.GetMethod("Foo", BindingFlags.NonPublic | BindingFlags.Instance);

            Assert.IsTrue((methodInfo.Attributes & MethodAttributes.Family) == MethodAttributes.Family);
        }
Ejemplo n.º 6
0
        public void StaticFieldIsStatic()
        {
            var provider        = new ReflectionProvider();
            var assemblyBuilder = new EmitAssemblyBuilder("TestAssembly");
            var typeBuilder     = assemblyBuilder.DefineType("", "TestClass");

            typeBuilder.DefineField("foo", typeof(string), isStatic: true);
            var type = provider.Compile(assemblyBuilder).GetType("TestClass");

            var field = type.GetField("foo");

            Assert.IsTrue(field.IsStatic);
        }
Ejemplo n.º 7
0
        public void StaticMethodIsStatic()
        {
            var provider        = new ReflectionProvider();
            var assemblyBuilder = new EmitAssemblyBuilder("TestAssembly");
            var typeBuilder     = assemblyBuilder.DefineType("", "TestClass");
            var method          = typeBuilder.DefineMethod("Foo", typeof(void), EmitVisibility.Public, isStatic: true);

            method.Il.Emit(EmitOpCodes.Ret);
            var type = provider.Compile(assemblyBuilder).GetType("TestClass");

            var methodInfo = type.GetMethod("Foo");

            Assert.IsTrue(methodInfo.IsStatic);
        }
Ejemplo n.º 8
0
        public void InternalMethodIsInternal()
        {
            var provider        = new ReflectionProvider();
            var assemblyBuilder = new EmitAssemblyBuilder("TestAssembly");
            var typeBuilder     = assemblyBuilder.DefineType("", "TestClass");
            var method          = typeBuilder.DefineMethod("Foo", typeof(void), EmitVisibility.Internal);

            method.Il.Emit(EmitOpCodes.Ret);
            var type = provider.Compile(assemblyBuilder).GetType("TestClass");

            var methodInfo = type.GetMethod("Foo", BindingFlags.NonPublic | BindingFlags.Instance);

            Assert.IsTrue((methodInfo.Attributes & MethodAttributes.Assembly) == MethodAttributes.Assembly);
        }
Ejemplo n.º 9
0
        public void SetAndGetValueFromField()
        {
            var provider        = new ReflectionProvider();
            var assemblyBuilder = new EmitAssemblyBuilder("TestAssembly");
            var typeBuilder     = assemblyBuilder.DefineType("", "TestClass");

            typeBuilder.DefineField("foo", typeof(string));
            var type     = provider.Compile(assemblyBuilder).GetType("TestClass");
            var instance = Activator.CreateInstance(type);
            var field    = type.GetField("foo");

            Assert.IsNull(field.GetValue(instance));
            field.SetValue(instance, "bar");
            Assert.AreEqual("bar", field.GetValue(instance));
        }
Ejemplo n.º 10
0
        public void MethodReturnsString()
        {
            var provider = new ReflectionProvider();
            var assemblyBuilder = new EmitAssemblyBuilder("TestAssembly");
            var typeBuilder = assemblyBuilder.DefineType("", "TestClass");
            var method = typeBuilder.DefineMethod("Foo", typeof(string));
            method.Il.Emit(EmitOpCodes.Ldstr, "bar");
            method.Il.Emit(EmitOpCodes.Ret);
            var type = provider.Compile(assemblyBuilder).GetType("TestClass");

            var instance = Activator.CreateInstance(type);
            var methodInfo = type.GetMethod("Foo");

            var result = (string)methodInfo.Invoke(instance, null);
            Assert.AreEqual("bar", result);
        }
Ejemplo n.º 11
0
        private MethodInfo CreateMethod(Action <EmitBlockStatement> ilGenerator, Type baseType = null, Action <EmitMethodBuilder> methodInstrumenter = null)
        {
            var provider        = new ReflectionProvider();
            var assemblyBuilder = new EmitAssemblyBuilder("MethodAssembly");
            var typeBuilder     = assemblyBuilder.DefineType("", "Method");
            var method          = typeBuilder.DefineMethod("Foo", typeof(object), isStatic: true);

            methodInstrumenter?.Invoke(method);
            var block = method.Body;

            ilGenerator(block);

            var assembly   = provider.Compile(assemblyBuilder);
            var type       = assembly.GetType(typeBuilder.FullName);
            var methodInfo = type.GetMethod("Foo");

            return(methodInfo);
        }
Ejemplo n.º 12
0
        public void MethodReturnsString()
        {
            var provider        = new ReflectionProvider();
            var assemblyBuilder = new EmitAssemblyBuilder("TestAssembly");
            var typeBuilder     = assemblyBuilder.DefineType("", "TestClass");
            var method          = typeBuilder.DefineMethod("Foo", typeof(string));

            method.Il.Emit(EmitOpCodes.Ldstr, "bar");
            method.Il.Emit(EmitOpCodes.Ret);
            var type = provider.Compile(assemblyBuilder).GetType("TestClass");

            var instance   = Activator.CreateInstance(type);
            var methodInfo = type.GetMethod("Foo");

            var result = (string)methodInfo.Invoke(instance, null);

            Assert.AreEqual("bar", result);
        }
Ejemplo n.º 13
0
        public void DeclareVariable()
        {
            var provider = new ReflectionProvider();
            var assemblyBuilder = new EmitAssemblyBuilder("TestAssembly");
            var typeBuilder = assemblyBuilder.DefineType("", "TestClass");
            var method = typeBuilder.DefineMethod("Foo", typeof(string));
            var variable = method.Il.DeclareLocal(typeof(string));
            method.Il.Emit(Ldstr, "bar");
            method.Il.Emit(Stloc, variable);
            method.Il.Emit(Ldloc, variable);
            method.Il.Emit(Ret);
            var type = provider.Compile(assemblyBuilder).GetType("TestClass");

            var instance = Activator.CreateInstance(type);
            var methodInfo = type.GetMethod("Foo");

            var result = (string)methodInfo.Invoke(instance, null);
            Assert.AreEqual("bar", result);
        }
Ejemplo n.º 14
0
        public void DeclareVariable()
        {
            var provider        = new ReflectionProvider();
            var assemblyBuilder = new EmitAssemblyBuilder("TestAssembly");
            var typeBuilder     = assemblyBuilder.DefineType("", "TestClass");
            var method          = typeBuilder.DefineMethod("Foo", typeof(string));
            var variable        = method.Il.DeclareLocal(typeof(string));

            method.Il.Emit(Ldstr, "bar");
            method.Il.Emit(Stloc, variable);
            method.Il.Emit(Ldloc, variable);
            method.Il.Emit(Ret);
            var type = provider.Compile(assemblyBuilder).GetType("TestClass");

            var instance   = Activator.CreateInstance(type);
            var methodInfo = type.GetMethod("Foo");

            var result = (string)methodInfo.Invoke(instance, null);

            Assert.AreEqual("bar", result);
        }
Ejemplo n.º 15
0
        public void SetAndGetValueFromField()
        {
            var provider = new ReflectionProvider();
            var assemblyBuilder = new EmitAssemblyBuilder("TestAssembly");
            var typeBuilder = assemblyBuilder.DefineType("", "TestClass");
            typeBuilder.DefineField("foo", typeof(string));
            var type = provider.Compile(assemblyBuilder).GetType("TestClass");
            var instance = Activator.CreateInstance(type);
            var field = type.GetField("foo");

            Assert.IsNull(field.GetValue(instance));
            field.SetValue(instance, "bar");
            Assert.AreEqual("bar", field.GetValue(instance));
        }
Ejemplo n.º 16
0
        public void StaticMethodIsStatic()
        {
            var provider = new ReflectionProvider();
            var assemblyBuilder = new EmitAssemblyBuilder("TestAssembly");
            var typeBuilder = assemblyBuilder.DefineType("", "TestClass");
            var method = typeBuilder.DefineMethod("Foo", typeof(void), EmitVisibility.Public, isStatic: true);
            method.Il.Emit(EmitOpCodes.Ret);
            var type = provider.Compile(assemblyBuilder).GetType("TestClass");

            var methodInfo = type.GetMethod("Foo");

            Assert.IsTrue(methodInfo.IsStatic);
        }
Ejemplo n.º 17
0
        public void StaticFieldIsStatic()
        {
            var provider = new ReflectionProvider();
            var assemblyBuilder = new EmitAssemblyBuilder("TestAssembly");
            var typeBuilder = assemblyBuilder.DefineType("", "TestClass");
            typeBuilder.DefineField("foo", typeof(string), isStatic: true);
            var type = provider.Compile(assemblyBuilder).GetType("TestClass");

            var field = type.GetField("foo");
            Assert.IsTrue(field.IsStatic);
        }
Ejemplo n.º 18
0
        private MethodInfo CreateMethod(Action<EmitBlockStatement> ilGenerator, Type baseType = null, Action<EmitMethodBuilder> methodInstrumenter = null)
        {
            var provider = new ReflectionProvider();
            var assemblyBuilder = new EmitAssemblyBuilder("MethodAssembly");
            var typeBuilder = assemblyBuilder.DefineType("", "Method");
            var method = typeBuilder.DefineMethod("Foo", typeof(object), isStatic: true);
            methodInstrumenter?.Invoke(method);
            var block = method.Body;
            ilGenerator(block);

            var assembly = provider.Compile(assemblyBuilder);
            var type = assembly.GetType(typeBuilder.FullName);
            var methodInfo = type.GetMethod("Foo");

            return methodInfo;
        }