C# 4.0 C#泛型接口类型返回Null
我有一个类C# 4.0 C#泛型接口类型返回Null,c#-4.0,asp.net-core,.net-standard,C# 4.0,Asp.net Core,.net Standard,我有一个类表,它返回实体的集合,例如: Table<Song> table = database.GetTable("SongCollection"); 或 ITable table=database.GetTable(“SongCollection”); 或 Table Table=database.GetTable(“SongCollection”); 或 Table Table=database.GetTable(“SongCollection”); 所有返回空值 我可
表
,它返回实体
的集合,例如:
Table<Song> table = database.GetTable("SongCollection");
或
ITable table=database.GetTable(“SongCollection”);
或
Table Table=database.GetTable(“SongCollection”);
或
Table Table=database.GetTable(“SongCollection”);
所有返回空值
我可以调用table.GetAll()
并返回一个列表
,然后将每个歌曲
转换成IEntity
即可
在
表
的情况下,我应该做些什么,这样我就不必返回列表
来获取表
,例如,当我创建一个管理界面来对所有表
执行CRUD操作时?这个解决方案实际上很奇怪,谢谢谢谢你帮我指引正确的方向
因此,我确实将输出类型从List
更改为IEnumerable
,代码中没有任何大的放大,我的终端界面看起来像这样:
public interface ITable<out T> where T : IEntity {
string TableName { get; set; }
Database CurrentDatabase { get; set; }
T New();
T Get(ulong id);
IEnumerable<T> GetAll();
void LoadAllStorageFiles(IEnumerable<IEntity> storage);
void StoreAllStorageFiles(IEnumerable<IEntity> storage);
void LoadRelated(IEntity current);
void LoadRelated(IEnumerable<IEntity> current);
}
公共接口ITable,其中T:IEntity{
字符串TableName{get;set;}
数据库CurrentDatabase{get;set;}
T新的();
T获取(ulongid);
IEnumerable GetAll();
void LoadAllStorageFiles(IEnumerable存储);
void StoreAllStorageFiles(IEnumerable存储);
空负荷相关(电流);
空荷载相关(IEnumerable current);
}
当我将协变方法和不变量(最初是逆变)方法分离为不同的接口类型时,协变方法将强制转换,逆变方法在强制转换期间将变为null
奇怪的是,这是我最初的逆变方法:
void LoadAllStorageFiles(IEnumerable<T> storage);
void StoreAllStorageFiles(IEnumerable<T> storage);
void LoadRelated(T current);
void LoadRelated(IEnumerable<T> current);
void LoadAllStorageFiles(IEnumerable存储);
void StoreAllStorageFiles(IEnumerable存储);
空荷载相关(T电流);
空荷载相关(IEnumerable current);
现在您可能会认为,将类型设置得更严格,只接受与表中其他部分相同的泛型类型,这是有隐含意义的。不过在本例中,在运行时它被视为反变量,而运行时只是放弃,并毫无例外地为null。在本例中,将参数设置为LoadRelated等,而不是T,无论暮光之城有多么严格,因为现在该参数已向任何方向开放,这似乎表明了一点,在某些逆变情况下,运行时环境可能会出现一些异常,但可能需要更多的代码、精力和金钱来解决这样的问题,满足运行时环境的检查,并且您能够顺利地继续前进
因此,我在这里只为遇到这种情况并稍微耸耸肩的任何其他人说几句话。也许有人可以在注释中解释一下。向我们展示函数。因为这4种类型(
ITable
,ITable
,Table
,Table
)都与Table
返回的Table
不相同。这些泛型类型定义未标记为协变的。如果它的泛型类型是只读的,您可以将它标记为协变的(使用out
关键字)。请参阅,我刚刚尝试了这一点,因此,一旦我将ITable的定义更改为public interface ITable,其中T:IEntity,它会导致接口的某些方法在方差方面出现错误。List GetAll();给出了一个关于不变性的错误,与无效载荷相关(T current);给出了一个关于逆变的错误。这些是否需要拆分为单独的接口?在我的接口声明中似乎有一些协变、不变和逆变方法。正确。如果你想知道为什么要考虑这个。如果这是可能的,并且您得到了一个实际类型为Table
的Table
实例,然后执行了Table.Add(newsomethingotherthantsong())这将破坏c#提供的类型安全性。这就是为什么类型必须是协变的,它声明您不能对集合进行更改,并确保强制转换表
不会导致错误,因为它不允许修改集合。
Table<Entity> table = database.GetTable("SongCollection");
Table<IEntity> table = database.GetTable("SongCollection");
public interface ITable<out T> where T : IEntity {
string TableName { get; set; }
Database CurrentDatabase { get; set; }
T New();
T Get(ulong id);
IEnumerable<T> GetAll();
void LoadAllStorageFiles(IEnumerable<IEntity> storage);
void StoreAllStorageFiles(IEnumerable<IEntity> storage);
void LoadRelated(IEntity current);
void LoadRelated(IEnumerable<IEntity> current);
}
void LoadAllStorageFiles(IEnumerable<T> storage);
void StoreAllStorageFiles(IEnumerable<T> storage);
void LoadRelated(T current);
void LoadRelated(IEnumerable<T> current);