C# 无法转换类型';委托类型';至';系统。代表';

C# 无法转换类型';委托类型';至';系统。代表';,c#,events,C#,Events,我需要像下面这样做,但是得到上面的错误 class PrioritizedEvent<DelegateType> { private ArrayList delegates; public PrioritizedEvent() { this.delegates = new ArrayList(); } public void AddDelegate(DelegateType d, int priority) {

我需要像下面这样做,但是得到上面的错误

class PrioritizedEvent<DelegateType>
{
    private ArrayList delegates;

    public PrioritizedEvent()
    {
        this.delegates = new ArrayList();
    }

    public void AddDelegate(DelegateType d, int priority)
    {
        this.delegates.Add(new PrioritizedDelegate<DelegateType>((Delegate)d,    priority));
        this.delegates.Sort();
    }

    protected class PrioritizedDelegate<DelegateType> : IComparable
    {
        public Delegate d;
        public int priority;

        public PrioritizedDelegate(Delegate d, int priority)
        {
            this.d = d;
            this.priority = priority;
        }
    }
}
类优先级
{
私人ArrayList代表;
公营部门
{
this.delegates=new ArrayList();
}
public void AddDelegate(delegated类型,int优先级)
{
this.delegates.Add(新的优先级数据legate((Delegate)d,priority));
this.delegates.Sort();
}
受保护的类Legate:IComparable
{
公共代表d;
公共优先权;
公共优先级数据授权(委托d,整数优先级)
{
这个。d=d;
优先权=优先权;
}
}
}

我无法将DelegateType D转换为Delegate

事实上,您无法指定
:Delegate
约束-这根本无法完成(编译器会阻止您)。您可能会发现在DelegateType:class中添加一个
很有用,只是为了停止使用
int
等,但您不能通过泛型来完成这一点。您需要通过
对象
进行强制转换:

(Delegate)(object)d
但是,我个人认为您应该存储
DelegateType
,而不是
Delegate
,即

protected class PrioritizedDelegate : IComparable
{
    public DelegateType d;
    public int priority;

    public PrioritizedDelegate(DelegateType d, int priority)
    {
        this.d = d;
        this.priority = priority;
    }
}
注意:我从上面删除了
:因为它嵌套在泛型类型(
prioritydevent
)中,所以它已经从父级继承了它

例如:

class PrioritizedEvent<TDelegateType> where TDelegateType : class
{
    private readonly List<PrioritizedDelegate> delegates
        = new List<PrioritizedDelegate>();

    public void AddDelegate(TDelegateType callback, int priority)
    {
        delegates.Add(new PrioritizedDelegate(callback, priority));
        delegates.Sort((x,y) => x.Priority.CompareTo(y.Priority));
    }

    protected class PrioritizedDelegate
    {
        public TDelegateType Callback {get;private set;}
        public int Priority {get;private set;}

        public PrioritizedDelegate(TDelegateType callback, int priority)
        {
            Callback = callback;
            Priority = priority;
        }
    }
}
class prioritydevent其中TDelegateType:class
{
私有只读列表委托
=新列表();
public void AddDelegate(TDelegateType回调,int优先级)
{
添加(新的优先级数据包(回调,优先级));
排序((x,y)=>x.Priority.CompareTo(y.Priority));
}
受保护的类的优先级
{
公共TDelegateType回调{get;private set;}
公共int优先级{get;私有集;}
公共优先级数据包(TDelegateType回调,int优先级)
{
回调=回调;
优先级=优先级;
}
}
}

事实上,您不能指定
:委托约束-这根本无法完成(编译器会阻止您)。您可能会发现在DelegateType:class
中添加一个
很有用,只是为了停止使用
int
等,但您不能通过泛型来完成这一点。您需要通过
对象
进行强制转换:

(Delegate)(object)d
但是,我个人认为您应该存储
DelegateType
,而不是
Delegate
,即

protected class PrioritizedDelegate : IComparable
{
    public DelegateType d;
    public int priority;

    public PrioritizedDelegate(DelegateType d, int priority)
    {
        this.d = d;
        this.priority = priority;
    }
}
注意:我从上面删除了
:因为它嵌套在泛型类型(
prioritydevent
)中,所以它已经从父级继承了它

例如:

class PrioritizedEvent<TDelegateType> where TDelegateType : class
{
    private readonly List<PrioritizedDelegate> delegates
        = new List<PrioritizedDelegate>();

    public void AddDelegate(TDelegateType callback, int priority)
    {
        delegates.Add(new PrioritizedDelegate(callback, priority));
        delegates.Sort((x,y) => x.Priority.CompareTo(y.Priority));
    }

    protected class PrioritizedDelegate
    {
        public TDelegateType Callback {get;private set;}
        public int Priority {get;private set;}

        public PrioritizedDelegate(TDelegateType callback, int priority)
        {
            Callback = callback;
            Priority = priority;
        }
    }
}
class prioritydevent其中TDelegateType:class
{
私有只读列表委托
=新列表();
public void AddDelegate(TDelegateType回调,int优先级)
{
添加(新的优先级数据包(回调,优先级));
排序((x,y)=>x.Priority.CompareTo(y.Priority));
}
受保护的类的优先级
{
公共TDelegateType回调{get;private set;}
公共int优先级{get;私有集;}
公共优先级数据包(TDelegateType回调,int优先级)
{
回调=回调;
优先级=优先级;
}
}
}

您的
委派类型
完全不受限制。编译器知道它可能是
int
或某个类或委托

现在,通常可以使用一些约束来限制泛型类型,不幸的是,不允许将其限制为delagate


回答为什么会给你一个解决办法。

你的
委托类型是完全不受限制的。编译器知道它可能是
int
或某个类或委托

现在,通常可以使用一些约束来限制泛型类型,不幸的是,不允许将其限制为delagate

“为什么”问题的答案为您提供了一个解决方案