【发布时间】:2012-11-30 23:35:20
【问题描述】:
我遇到了泛型问题:
public interface IEntity {}
public class User : IEntity {}
public class Experiments {
private IList<IEntity> list;
private IList<Action<IEntity>> actions;
public Experiments(){
list = new List<IEntity>();
actions = new List<Action<IEntity>>();
}
public void Add<T>(T entity) where T : IEntity {
list.Add(entity); // -> NO PROBLEM HERE
}
public void AddAction<T>(Action<T> handle) where T : IEntity {
actions.Add(handle); // -> HERE I GET AN ERROR
}
为什么我得到"cannot convert from 'System.Action
【问题讨论】:
-
IEntity!=IEntityCheck -
您是否复制了正确的错误信息?当我编译它时,我得到:'System.Collections.Generic.ICollection
>.Add(System.Action )' 的最佳重载方法匹配有一些无效参数 -
如果你知道
T是一个IEntity,为什么不使用IEntity而不是泛型呢? -
@JohnKalberer 查看我在 JG in SD 的回答下的评论。
标签: c# asp.net c#-4.0 generics interface