문제

'Mixed'클래스 인스턴스의 메소드를 가리키는 .NET 대표를 초기화하는 방법을 아는 사람이 있습니까?

다음과 같은 '혼합'C ++ 클래스가 있습니다.

class CppMixClass
{
public:
    CppMixClass(void){
        dotNETclass->StateChanged += gcnew DotNetClass::ServiceStateEventHandler(&UpdateHealthState);
    }
   ~CppMixClass(void);
   void UpdateState(System::Object^ sender, DotNetClass::StateEventArgs^ e){
       //doSmth
   }
}

DotNetClass는 C#에서 구현되며 메소드 선언은 대의원에서는 괜찮습니다. 이 줄은 오류를 생성합니다.

dotNETclass->StateChanged += gcnew DotNetClass::ServiceStateEventHandler(&UpdateHealthState);
error C2276: '&' : illegal operation on bound member function expression

누구든지 문제에 대한 단서가 있습니까? CoZ CPPMixClass 클래스가 순수한 .NET (ref) 클래스가 아닐 수도 있습니까?

UpdateHealthstate가 정적 메소드 일 때이 작업을 수행했지만 인스턴스 메소드에 대한 포인터가 필요합니다.

나는 SMTH를 시도했다 :

dotNETclass->StateChanged += gcnew DotNetClass::ServiceStateEventHandler(this, &UpdateHealthState);

그러나 이것은 분명히 작동하지 않습니다. 이것은 포인터 (핸들)에 대한 .NET (ref) 클래스 (System :: Object)가 아닙니다.

servicestateeventhandler는 c#에 다음과 같이 정의됩니다.

public delegate void ServiceStateEventHandler(object sender, ServiceStateEventArgs e);

이것을 읽는 고맙습니다 :)

도움이 되었습니까?

해결책

방금 이것에 대한 대답을 찾았습니다 (물론 Nishant Sivakumar의 Man은 모든 C ++/CLI Interop 관련 문제에 대한 답변이있는 것 같습니다).

http://www.codeproject.com/kb/mcpp/cppclisupportlib.aspx?display=print

답변은 "MSCLR/Event.H"헤더에 있으며, 여기서 기본 클래스의 대표를위한 매크로가 정의됩니다.

Nish의 코드는 다음과 같습니다.

class Demo5
{
msclr::auto_gcroot<FileSystemWatcher^> m_fsw;
public:
// Step (1)
// Declare the delegate map where you map
// native method to specific event handlers

BEGIN_DELEGATE_MAP(Demo5)
    EVENT_DELEGATE_ENTRY(OnRenamed, Object^, RenamedEventArgs^)
END_DELEGATE_MAP()

Demo5()
{
    m_fsw = gcnew  FileSystemWatcher("d:\\tmp");
    // Step (2)
    // Setup event handlers using MAKE_DELEGATE
    m_fsw->Renamed += MAKE_DELEGATE(RenamedEventHandler, OnRenamed);
    m_fsw->EnableRaisingEvents = true;
}
// Step (3)
// Implement the event handler method

void OnRenamed(Object^, RenamedEventArgs^ e)
{
    Console::WriteLine("{0} -> {1}",e->OldName, e->Name);
}
};

다른 팁

.NET 유형 만 이벤트를 사용할 수 있습니다. 이벤트를 처리하고 CPPMIXClass 내에서 해당 클래스를 작성하는 새로운 관리 클래스를 작성하고 건설 중에 CPPMixClass에 대한 포인터를 전달하는 것이 좋습니다. 관리 이벤트 처리 클래스는 이벤트를 처리 할 때 CPPMixClass에서 함수를 호출 할 수 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top