有一个类和一个委托 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();
}
};
哪里有问题?