我想要一个引用委托的参考,该委托返回一个泛型接口的实例,其中类型参数是协变的。如果我在调用Get时指定了T的类型,则编译正常。但是在Get内部,我无法进行看似等效的赋值 - 尽管T被限制为IItem。
它说...
无法隐式将类型'System.Func'转换为'System.Func>'。存在一个显式转换(是否缺少强制转换?)
为什么会这样呢?
它说...
无法隐式将类型'System.Func'转换为'System.Func>'。存在一个显式转换(是否缺少强制转换?)
为什么会这样呢?
using System;
using System.Collections.Generic;
using System.Linq;
namespace MyNamespace
{
public interface IA<out T>
{
IEnumerable<T> f();
}
public class A<T> : IA<T>
{
public IEnumerable<T> f()
{
return Enumerable.Empty<T>();
}
}
public interface IItem {}
public class Item : IItem {}
public static class Program
{
public static void Main(string[] args)
{
Func<IA<IItem>> a = Get<Item>();
}
public static Func<IA<T>> Get<T>() where T : IItem
{
var x = () => new A<T>();
//Doesn't compile
Func<IA<IItem>> a = x;
return x;
}
}
}