diff Examples/Cpp/HelloWorld.cpp @ 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/Cpp/HelloWorld.cpp	Thu Mar 27 21:46:09 2014 +0400
@@ -0,0 +1,57 @@
+//@ example:
+//@ emit Emit
+#include "stdafx.h"
+
+using namespace System;
+
+using namespace NUnit::Framework;
+
+using namespace BLToolkit::Reflection;
+using namespace BLToolkit::Reflection::Emit;
+
+namespace Examples {
+namespace Reflection {
+namespace Emit
+{
+	[TestFixture]
+	public ref class HelloWorld
+	{
+	public:
+
+		interface class IHello
+		{
+			void SayHello(String ^toWhom);
+		};
+
+		[Test]
+		void Test()
+		{
+			AssemblyBuilderHelper ^assembly = gcnew AssemblyBuilderHelper("HelloWorld.dll");
+			
+			EmitHelper ^emit = assembly
+				->DefineType  ("Hello", Object::typeid, IHello::typeid)
+				->DefineMethod(IHello::typeid->GetMethod("SayHello"))
+				->Emitter;
+
+			emit
+				// string.Format("Hello, {0} World!", toWhom)
+				//
+				->ldstr   ("Hello, {0} World!")
+				->ldarg_1
+				->call    (String::typeid, "Format", String::typeid, Object::typeid)
+
+				// Console.WriteLine("Hello, World!");
+				//
+				->call    (Console::typeid, "WriteLine", String::typeid)
+				->ret()
+				;
+
+			Type ^type = emit->Method->Type->Create();
+
+			IHello ^hello = (IHello^)TypeAccessor::CreateInstance(type);
+
+			hello->SayHello("C++");
+		}
+	};
+}}}
+