2
有一個類和一個代表C#麻煩與代表
public delegate void Super();
public class Event
{
public event Super activate ;
public void act()
{
if (activate != null) activate();
}
}
和C++/CLI
public delegate void Super();
public ref class Event
{
public:
event Super ^activate;
void act()
{
activate();
}
};
在C#
創建在這樣的(方法Setplus和setminus)
類多播委託public class ContainerEvents
{
private Event obj;
public ContainerEvents()
{
obj = new Event();
}
public Super Setplus
{
set { obj.activate += value; }
}
public Super Setminus
{
set { obj.activate -= value; }
}
public void Run()
{
obj.act();
}
}
但在C++/Cli中出現錯誤 - usage requires Event::activate to be a data member
public ref class ContainerEvents
{
Event ^obj;
public:
ContainerEvents()
{
obj = gcnew Event();
}
property Super^ Setplus
{
void set(Super^ value)
{
obj->activate = static_cast<Super^>(Delegate::Combine(obj->activate,value));
}
}
property Super^ SetMinus
{
void set(Super^ value)
{
obj->activate = static_cast<Super^>(Delegate::Remove(obj->activate,value));
}
}
void Run()
{
obj->act();
}
};
問題在哪裏?