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();
     }
};

哪里有问题?

4

1 回答 1

2

请参阅:http: //msdn.microsoft.com/en-us/library/ms235237 (v=vs.80).aspx

C++/CLI 遵循与 C# 相同的类比。在 C# 中定义它是非法的:

public Super Setplus
{
    set { obj.activate = Delegate.Combine(obj.activate, value); }
}

C++/CLI 也是如此。使用现代语法中定义的 +=/-= 表示法。

property Super^ Setplus
{
    void set(Super^ value)
    {
        obj->activate += value;
    }
}
于 2012-05-06T04:33:57.333 回答