2014-12-12 5 views
1

리플렉션 Invoke를 사용하여 int * 인수로 함수를 호출 할 수 있습니까? 리플렉션 사용하기

내 꿈 코드 : 호출하는

기능 :

long Invoked_Function(Int32, Int32*); 

그것은

cannot convert from 'int *' to 'System::Object ^' 
"배열^Parameters_Objects = ..."문자열에 대해 말해 sying

Int32 First_Parameter; 
Int32 Second_Parameter; 
Int32* Second_Parameter_Pointer = &Second_Parameter; 
array<Object^>^ Parameters_Objects = gcnew array<Object^>(2){ First_Parameter, Second_Parameter_Pointer}; 
long Result = (long)Function_Type->Invoke(Class_Instace, Parameters_Objects); 

호출하는 코드

이유를 이해할 수 있습니다.

그리고 여기 내 질문 : invoke 함수를 사용하여 인수 중 하나로 primite 아닌 형식의 포인터를 사용하여 함수를 호출 할 수 있습니까?

답변

3

정확합니다. 네이티브 포인터에 대한 복싱 변환은 없습니다. 리플렉션은 기꺼이 IntPtr을 대신 받아들입니다. 다음을 보여주는 샘플 프로그램 :

#include "stdafx.h" 

using namespace System; 
using namespace System::Reflection; 

ref class Example { 
public: 
    long Invoked_Function(Int32 a, Int32* pb) { 
     Console::WriteLine("Invoked with {0}, {1}", a, *pb); 
     return 999; 
    } 
}; 

int main(array<System::String ^> ^args) 
{ 
    auto obj = gcnew Example; 
    auto mi = obj->GetType()->GetMethod("Invoked_Function"); 
    int b = 666; 
    int* ptr = &b; 
    array<Object^>^ arg = gcnew array <Object^> {42, IntPtr(ptr)}; 
    long result = (long)mi->Invoke(obj, arg); 
    Console::WriteLine("Result = {0}", result); 
    return 0; 
}