Invoke a method : Method « Reflection « Visual C++ .NET






Invoke a method

 
#include "stdafx.h"

using namespace System;
using namespace System::Reflection;

ref class Reflector
{
   public:

   void TestDynamicCall(String^ greeting)
   {
      Console::WriteLine(greeting);
   }

};
void LoadAndReflect(String^ assemblyFileName, String^ typeName,String^ methodName, array<Object^>^ parameterList)
{
      Assembly^ assembly = Assembly::LoadFrom(assemblyFileName);
      Type^ t= assembly->GetType(typeName);
      MethodInfo^ method = t->GetMethod(methodName);
      Object^ obj = Activator::CreateInstance(t);
      method->Invoke(obj, parameterList);

}

int main()
{
   array<Object^>^ params = gcnew array<Object^> { "Hello!" };
   LoadAndReflect("reflection2.exe", "Reflector", "TestDynamicCall", params);
}

   
  








Related examples in the same category

1.Using reflection to get the method return type
2.Using reflection to get the parameter info of a method
3.Get method from Type