Beispiel #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);            
        }
Beispiel #2
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));
        }
Beispiel #3
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);
        }
Beispiel #4
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);
        }
Beispiel #5
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);
        }
Beispiel #6
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));
        }
Beispiel #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);
        }
Beispiel #8
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);
        }
Beispiel #9
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;
        }