Visual C++ .NET/Reflection/Method — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:05, 26 мая 2010
Содержание
Get method from Type
#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);
}
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);
}
Using reflection to get the method return type
#include "stdafx.h"
using namespace System;
using namespace System::Reflection;
ref class Reflector
{
public:
void LoadAndReflect(String^ assemblyFileName)
{
Assembly^ assembly = Assembly::LoadFrom(assemblyFileName);
array<Type^>^ types = assembly->GetTypes();
for each (Type^ t in types)
{
Console::WriteLine(t->ToString());
array<MethodInfo^>^ methods = t->GetMethods();
for each (MethodInfo^ method in methods)
{
Console::Write(" {0} {1}(", method->ReturnType->ToString(),method->Name);
array<ParameterInfo^>^ params = method->GetParameters();
for (int i = 0; i < params->Length; i++)
{
ParameterInfo^ param = params[i];
Console::Write("{0} {1}", param->ParameterType->ToString(), param->Name);
if (i < params->Length - 1)
Console::Write(", ");
}
Console::WriteLine(")");
}
}
}
};
int main(array<String^>^ args)
{
Reflector^ r = gcnew Reflector();
for each (String^ s in args)
{
Console::WriteLine("Reflection on {0}", s);
r->LoadAndReflect(s);
}
}
Using reflection to get the parameter info of a method
#include "stdafx.h"
using namespace System;
using namespace System::Reflection;
ref class Reflector
{
public:
void LoadAndReflect(String^ assemblyFileName)
{
Assembly^ assembly = Assembly::LoadFrom(assemblyFileName);
array<Type^>^ types = assembly->GetTypes();
for each (Type^ t in types)
{
Console::WriteLine(t->ToString());
array<MethodInfo^>^ methods = t->GetMethods();
for each (MethodInfo^ method in methods)
{
Console::Write(" {0} {1}(", method->ReturnType->ToString(),method->Name);
array<ParameterInfo^>^ params = method->GetParameters();
for (int i = 0; i < params->Length; i++)
{
ParameterInfo^ param = params[i];
Console::Write("{0} {1}", param->ParameterType->ToString(), param->Name);
if (i < params->Length - 1)
Console::Write(", ");
}
Console::WriteLine(")");
}
}
}
};
int main(array<String^>^ args)
{
Reflector^ r = gcnew Reflector();
for each (String^ s in args)
{
Console::WriteLine("Reflection on {0}", s);
r->LoadAndReflect(s);
}
}