///////////////////////////////////////////////////////////////////// // Test.cpp - Demonstrate testing native C++ using reflection via // // managed C++/CLI test driver // // // // Jim Fawcett, CSE784 - Software Studio, Fall 2008 // ///////////////////////////////////////////////////////////////////// #include "Test.h" int main(int argc, char* argv[]) { Console::Write("\n Testing native C++ from C++/CLI driver using Reflection"); Console::Write("\n =========================================================\n"); ///////////////////////////////////////////////////////// // Direct demonstration of test class Test^ t = gcnew Test(); t->say(); t->sayMore("Hi"); ///////////////////////////////////////////////////////// // Test using reflection Type^ tc = t->GetType(); if(tc->IsClass) Console::Write("\n {0} is a class",tc->Name); else Console::Write("\n {0} is not a class", tc->Name); // should succeed Type^ Interface1 = tc->GetInterface("ITest"); if(Interface1 != nullptr) Console::Write("\n found interface {0}",Interface1->Name); else Console::Write("\n last GetInterface failed"); // should fail Type^ Interface2 = tc->GetInterface("IFoobar"); if(Interface2 != nullptr) Console::Write("\n found interface {0}",Interface1->Name); else Console::Write("\n last GetInterface failed"); // create instance (as if accessed through loaded assembly ITest^ tester = (ITest^)Activator::CreateInstance(tc); if(tester->test()) Console::Write("\n test passed"); else Console::Write("\n test failed"); // testing things not needed in test harness try { MethodInfo^ mi = tc->GetMethod("say",BindingFlags::Instance | BindingFlags::Public); Console::Write("\n method name is {0}", mi->Name); mi->Invoke(tester, nullptr); mi = tc->GetMethod("sayMore",BindingFlags::Instance | BindingFlags::Public); Console::Write("\n method name is {0}", mi->Name); mi->Invoke(tester, gcnew array { "Hi" }); } catch(Exception^ ex) { Console::Write("\n {0}\n\n",ex->Message); } Console::Write("\n\n"); }