Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 C#泛型接口类型返回Null_C# 4.0_Asp.net Core_.net Standard - Fatal编程技术网

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())
不会导致错误,因为它不允许修改集合。
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);