late-bound


cat late-bound_test.cpp
// late-bound_test.cpp
// console application

#using <mscorlib.dll>
#using <System.dll>

using namespace System;
using namespace System::Reflection;
using namespace System::Security::Permissions;

#include "late-bound_test.h"

public ref class Example
{
private: 
    int factor;

public:
    Example(int f)
    {
        factor = f;
    }

    int SampleMethod(int x) 
    { 
        Console::WriteLine("\nExample->SampleMethod({0}) executes.", x);
        return x * factor;
    }
};

int main(array<System::String ^> ^args){
	Console::WriteLine("HelloWorld\n");
    Assembly^ assem = Assembly::GetExecutingAssembly();

    // Create an object from the assembly, passing in the correct number and
    // type of arguments for the constructor.
    Object^ o = assem->CreateInstance("Example", false, 
        BindingFlags::ExactBinding, 
        nullptr, gcnew array<Object^> { 2 }, nullptr, nullptr);

    // Make a late-bound call to an instance method of the object.    
    MethodInfo^ m = assem->GetType("Example")->GetMethod("SampleMethod");
    Object^ ret = m->Invoke(o, gcnew array<Object^> { 42 });
    Console::WriteLine("SampleMethod returned {0}.", ret);

    Console::WriteLine("\nAssembly entry point:");
    Console::WriteLine(assem->EntryPoint);
	return EXIT_SUCCESS;
}


cat makefile
# makefile for late-bound_test
# console application

.PHONY: all test mmm clean 
.SUFFIXES: .exe .obj .txt .resources .dll

TARGET=late-bound_test

CULTUREDIRS=
DLLS=

all: $(TARGET).exe $(DLLS)


LOPT=

OBJS=$(TARGET).obj AssemblyInfo.obj

$(TARGET).obj: $(TARGET).cpp


AssemblyInfo.obj: AssemblyInfo.cpp

meta: $(TARGET).exe
	ILDasm /out:$@ /metadata $<

$(TARGET).exe: $(OBJS)  
	link $^ /out:$@ $(LOPT)
	@echo

.cpp.obj:
	cl -c -Wall -Od -clr:safe $<
	@echo

test: $(TARGET).exe
	./$(TARGET).exe 

clean:
	@-rm $(TARGET).exe *.obj *~ $(TARGET).exe.manifest 1>/dev/null 2>&1
	@-rm *.resources 1>/dev/null 2>&1
	@-rm -fr $(CULTUREDIRS) 1>/dev/null 2>&1
	@-rm meta 1>/dev/null 2>&1

mmm:
	cat $(TARGET).cpp
	cat AssemblyInfo.cpp
	@echo
	cat makefile

html:
	cat /dev/clipboard | code2html.exe | unix2dos

install: $(TARGET).exe
	cp $(TARGET).exe ~/bin
	cp $(TARGET).exe.manifest ~/bin