【发布时间】:2015-05-18 01:46:04
【问题描述】:
考虑以下代码:
namespace TestNameSpace {
public interface IFinder
{
IEnumerable<T> GetData<T>(DataStore dataStore);
}
public class EmployeeFinder : IFinder {
public IEnumerable<Employee> GetData<Employee>(DataStore dataStore) {
return dataStore.Employees; //*****This causes an error*****
}
}
public class DataStore {
public IEnumerable<Employee> Employees { get; set;}
}
public class Employee {
}
}
我返回 dataStore.Employees 的行导致编译错误。
无法将类型“System.Collections.Generic.IEnumerable
”隐式转换为“System.Collections.Generic.IEnumerable ”。存在显式转换(您是否缺少演员表?)
为什么当它们是相同类型时需要隐式转换?为什么一个是命名空间而另一个不是?
谢谢!
【问题讨论】:
-
您在不同的命名空间中有两种不同类型的同名 (
Employee)。查看您的代码库是否缺少命名空间声明或意外重新定义的类。