C#库中函数如下:
public static bool ShowWaitDialog(string prompt, CanCloseFormDelegate canClose)
第二个参数是一个委托,定义如下:
public delegate int CanCloseFormDelegate();
现在要从VC CLI中调用C#库的这个函数:
int uTest::Test::SwitchOn()
{
bool b = XXX::XXX::ShowWaitDialog( "Power on.", CheckIt );
if( !b ) return -1;
return 0;
}
int uTest::Test::CheckIt()
{
return 0;
}
其中第二个参数该如何传递?目前编译不通过,不知采取何种形式。
------解决方案--------------------
如果uTest::Test是托管类(ref class定义),用
gcnew CanCloseFormDelegate(this, &uTest::Test::CheckIt)
如果不是(class定义)
创建一个托管类包装它
------解决方案--------------------
msdn
public delegate void f( int );
public ref struct E {
private:
f^ _E; // delegates are also reference types
public:
E() { // note the replacement of 0 with nullptr!
_E = nullptr;
}
// the new aggregate syntax of an explicit event declaration
event f^ E1 {
public:
void add( f^ d ) {
_E += d;
}
protected:
void remove( f^ d ) {
_E -= d;
}
private:
void raise( int i ) {
if ( _E )
_E( i );
}
}
static void Go() {
E^ pE = gcnew E;
pE->E1 += gcnew f( pE, &E::handler );
pE->E1( 17 );
pE->E1 -= gcnew f( pE, &E::handler );
pE->E1( 17 );
}
};