diff Examples/CS/Reflection.Emit/HelloWorldNormal.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Examples/CS/Reflection.Emit/HelloWorldNormal.cs	Thu Mar 27 21:46:09 2014 +0400
@@ -0,0 +1,63 @@
+using System;
+using System.Reflection;
+using System.Reflection.Emit;
+using System.Threading;
+
+using NUnit.Framework;
+
+namespace Examples.Reflection.Emit
+{
+	[TestFixture]
+	public class HelloWorldNormal
+	{
+		public interface IHello
+		{
+			void SayHello(string toWhom);
+		}
+
+		[Test]
+		public void Test()
+		{
+			AssemblyName asmName = new AssemblyName();
+
+			asmName.Name = "HelloWorld";
+
+			AssemblyBuilder asmBuilder =
+				Thread.GetDomain().DefineDynamicAssembly(asmName, AssemblyBuilderAccess.RunAndSave);
+
+			ModuleBuilder   modBuilder  = asmBuilder.DefineDynamicModule("HelloWorld");
+
+			TypeBuilder     typeBuilder = modBuilder.DefineType(
+				"Hello",
+				TypeAttributes.Public,
+				typeof(object),
+				new Type[] { typeof(IHello) });
+
+			MethodBuilder   methodBuilder = typeBuilder.DefineMethod("SayHello", 
+									MethodAttributes.Private | MethodAttributes.Virtual,
+									typeof(void), 
+									new Type[] { typeof(string) });
+
+			typeBuilder.DefineMethodOverride(methodBuilder, typeof(IHello).GetMethod("SayHello"));
+
+			ILGenerator il = methodBuilder.GetILGenerator();
+
+			// string.Format("Hello, {0} World!", toWhom)
+			//
+			/*[a]*/il.Emit/*[/a]*/(OpCodes.Ldstr, "Hello, {0} World!");
+			/*[a]*/il.Emit/*[/a]*/(OpCodes.Ldarg_1);
+			/*[a]*/il.Emit/*[/a]*/(OpCodes.Call, typeof(string).GetMethod("Format", new Type[] { typeof(string), typeof(object) }));
+
+			// Console.WriteLine("Hello, World!");
+			//
+			/*[a]*/il.Emit/*[/a]*/(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }));
+			/*[a]*/il.Emit/*[/a]*/(OpCodes.Ret);
+
+			Type   type  = typeBuilder.CreateType();
+
+			IHello hello = (IHello)Activator.CreateInstance(type);
+
+			hello.SayHello("Emit");
+		}
+	}
+}