我无法理解为什么下面的代码片段没有给我一个错误
public void SomeMethod<T>(T arg) where T : MyInterface
{
MyInterface e = arg;
}
但是这个,由于泛型类型约束,我希望它能工作
private readonly IList<Action<MyInterface>> myActionList = new List<Action<MyInterface>>();
public IDisposable Subscribe<T>(Action<T> callback) where T: MyInterface
{
myActionList.Add(callback); // doesn't compile
return null
}
给出这个错误
cannot convert from 'System.Action<T>' to 'System.Action<MyInterface>'
我正在使用 VS2012 sp1 和 .NET 4.5。
谁能解释为什么约束不允许它编译?
最佳答案
这是一个逆变问题 - Action<MyInterface>
应该能够采取任何 MyInterface
实例作为参数,但是您正在尝试存储 Action<T>
其中 T
是 MyInterface
的一些子类型,这是不安全的。
例如,如果您有:
public class SomeImpl : MyInterface { }
public class SomeOtherImpl : MyInterface { }
List<Action<MyInterface>> list;
list.Add(new Action<SomeImpl>(i => { }));
ActionMyInterface act = list[0];
act(new SomeOtherImpl());
您只能分配 Action<T>
给一些Action<U>
如果类型 T
比 U
类型“更小” .例如
Action<string> act = new Action<object>(o => { });
是安全的,因为字符串参数在对象参数所在的地方始终有效。
关于c# - Action 的通用约束没有按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15610892/