从合成

时间:2016-05-29 09:08:11

标签: c#

我有这个代码应该根据某些逻辑组成一个委托。出于某种原因,我无法从作文中删除一名代表。

例如:

delegate void MyDelegate();// This is defined in my delegate handler class where ComposeDelegates is
      void ComposeDelegates()
            {
                MyDelegate c = new MyDelegate(destroy + showPoints);//For example this will call Destroy and Show points;

                c = c - showPoints;// This does nothing. If I do c = c +showpoints it will call showpoints twice.
                c();// This calls the functions asociated with the destroy and showPoints delegates.

            }

void ComposeDelegates()// This works as expected;
    {
       switch(type)
        {
            case BehaviourType.JustDestroy:
                c += destroy;
                break;
            case BehaviourType.DestroyAndShowPoints:
                c = destroy + showPoints;
                break;
            case BehaviourType.ShowPoints:
                c += showPoints;
                break;
        }

        c();  
    }
void AnotherWayToComposeDelegates()// This works as well.
        {
            c = destroy + showPoints;
            //Lets say I want to remove showPoints;
            if (type == BehaviourType.JustDestroy)
            {
                c -= showPoints;
            }
            c();
        }
    }

为什么+工作但不是 - 符号。如何从c中删除showPoints?

编辑:它是否可能无法正常工作,因为这是一个Unity3d项目,他们正在使用旧版本的.Net。但是又为什么它编译没有错误?

编辑2:问题似乎是出于某种原因用新的Mydelegate()实例化了delega c。

2 个答案:

答案 0 :(得分:0)

我认为您只需要修复语法以使代码正常工作。

public delegate void MyDelegate(); // Delegate declaration - it basically a function signature, nothing else.

public void ComposeDelegates()
{
    MyDelegate c = Destroy; // Creates a delegate
    c += ShowPoints; // add ShowPoints to invocation list
    c -= ShowPoints; // Remove ShowPoints from invocation list
    c.Invoke(); // Calls delegate, i.e. invokes all methods attached to delegate.
}

public void Destroy()
{
}

public void ShowPoints()
{
}

ATTEMPT 2:

public enum BehaviourType
{
    JustDestroy,
    DestroyAndShowPoints,
    ShowPoints
}

public static class MyDelegatesFactory
{
    public static Action Create(BehaviourType type,
        Action destroy, Action showPoints)
    {
        Action result = null;
        switch(type)
        {
            case BehaviourType.JustDestroy:
                result = destroy;
                break;
            case BehaviourType.DestroyAndShowPoints:
                result = destroy;
                result += showPoints;
                break;
            case BehaviourType.ShowPoints:
                result = showPoints;
                break;
        }
        return result;
    }
}

代码中的某处:

var myDelegates = MyDelegatesFactory.Create(
    BehaviourType.DestroyAndShowPoints, Destroy, ShowPoints);

if (myDelegates != null)
{
    myDelegates.Invoke();    
}

答案 1 :(得分:-1)

您可以使用Delegate.Combine和Delegate.Remove静态方法:

delegate void MyDelegate();// This is defined in my delegate handler class where ComposeDelegates is
  void ComposeDelegates()
        {
            var c = Delegate.Combine(destroy,showPoints);

            c = Delegate.Remove(c ,showPoints);
            c();// This calls the functions asociated with the destroy and showPoints delegates.

        }

void ComposeDelegates()// This works as expected;
{
   switch(type)
    {
        case BehaviourType.JustDestroy:
            c = (MyDelegate)Delegate.Combine(c,destroy);
            break;
        case BehaviourType.DestroyAndShowPoints:
            c = (MyDelegate)Delegate.Combine(destroy,showPoints);        break;
        case BehaviourType.ShowPoints:
            c=  (MyDelegate)Delegate.Combine(c,showPoints);
            break;
    }

    c();  
}
void AnotherWayToComposeDelegates()// This works as well.
    {
        c = (MyDelegate)Delegate.Combine(destroy,showPoints); 
        //Lets say I want to remove showPoints;
        if (type == BehaviourType.JustDestroy)
        {
            c =(MyDelegate) Delegate.Remove(c, showPoints); ;
        }
        c();
    }
}