コード例 #1
0
ファイル: IlTests.cs プロジェクト: kswoll/sexy-emit
        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);            
        }
コード例 #2
0
ファイル: BasicTests.cs プロジェクト: kswoll/sexy-emit
        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));
        }
コード例 #3
0
ファイル: BasicTests.cs プロジェクト: kswoll/sexy-emit
        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);
        }
コード例 #4
0
ファイル: BasicTests.cs プロジェクト: kswoll/sexy-emit
        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);
        }
コード例 #5
0
ファイル: IlTests.cs プロジェクト: kswoll/sexy-emit
        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);
        }
コード例 #6
0
ファイル: BasicTests.cs プロジェクト: kswoll/sexy-emit
        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));
        }
コード例 #7
0
ファイル: BasicTests.cs プロジェクト: kswoll/sexy-emit
        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);
        }
コード例 #8
0
ファイル: BasicTests.cs プロジェクト: kswoll/sexy-emit
        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);
        }
コード例 #9
0
ファイル: EmitAstTests.cs プロジェクト: kswoll/sexy-emit
        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;
        }