2012-05-06 5 views
2

あり、クラスやデリゲートのC#トラブル

public delegate void Super(); 
public class Event 
{ 
    public event Super activate ; 
    public void act() 
    { 
     if (activate != null) activate(); 
    } 
} 

およびC++/CLI C#で

public delegate void Super(); 
public ref class Event 
{ 
public: 
    event Super ^activate; 
    void act() 
    { 
     activate(); 
    } 
}; 

私は(メソッド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(); 
    } 
}; 

問題はどこですか?

答えて

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