Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.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# 力可观测集合<;T>;集合以获取特定类型_C#_.net 3.5 - Fatal编程技术网

C# 力可观测集合<;T>;集合以获取特定类型

C# 力可观测集合<;T>;集合以获取特定类型,c#,.net-3.5,C#,.net 3.5,我的基类中有以下通用方法: protected void Show<T>(ObservableCollection<T> collection) { StatusText = Resource.loadingData; if (collection is ObservableCollection<Language>) { collection = DBService.GetLanguages(); } S

我的基类中有以下通用方法:

protected void Show<T>(ObservableCollection<T> collection)
{
    StatusText = Resource.loadingData;
    if (collection is ObservableCollection<Language>)
    {
        collection = DBService.GetLanguages();
    }
    StatusText = string.Empty;
}
protected void Show(可观测集合)
{
StatusText=Resource.loadingData;
如果(收集是可观察的收集)
{
collection=DBService.GetLanguages();
}
StatusText=string.Empty;
}
如果集合的类型是Language,我想强制集合引用DBService.GetLanguages()的return
observeCollection

但编译器说:

无法在“System.Collections.ObjectModel.ObservableCollection”中隐式转换“System.Collections.ObjectModel.ObservableCollection”


我猜您在这个方法中使用了
collection
(可能是显示成员),但是在
Show
方法中重新分配集合肯定不是一个好主意。检查方法外部的类型
语言
,并传递正确的集合以显示


顺便说一句:您需要检查
T
类型语言,而不是集合。

我想您的意思是
集合是可观察的集合
,您还需要使用
ref
参数传递集合。否则该值将无法保存。请参阅Hey Ned Stoyanov和okrumnow,谢谢您的回答!我已经将if条件修改为if(collection是observectablecollection)。如果(T是语言)不起作用。。。然而,如果我在if语句中,我知道集合可以指向或保存其他ObservableCollection,为什么我不能编译它呢?您不能编译它,因为编译器不知道什么类型是
t
,对于解决方案,您可以转换为
ObservableCollection
,比如
collection=DBService.GetLanguages()作为可观察的集合非常感谢你,格伦迪!!!!但我理解的最后一个问题是:为什么编译器不允许这样的代码:if(t是语言)或if(typeof(t)是语言)