Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.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# 创建通用列表驱动的表单可观察集合_C# - Fatal编程技术网

C# 创建通用列表驱动的表单可观察集合

C# 创建通用列表驱动的表单可观察集合,c#,C#,我想构建一个从基本可观察集合派生的可观察集合,以便根据集合包含的子对象的类型执行某些操作 public class DbListBase : ObservableCollection<T> { public DbListBase() { if (TypeOf(T) == Machine) {Do This} if (TypeOf(T) == Person) {Do That} } } 我如何才能使第一种方法DbListBas

我想构建一个从基本
可观察集合
派生的
可观察集合
,以便根据集合包含的子对象的类型执行某些操作

public class DbListBase : ObservableCollection<T>
{
    public DbListBase()
    {
        if (TypeOf(T) == Machine) {Do This}

        if (TypeOf(T) == Person) {Do That}
    }
}

我如何才能使第一种方法
DbListBase:observateCollection
起作用?

导致错误的直接原因是:您的类也必须是泛型的:

public class DbListBase<T> : ObservableCollection<T>
               // here ^^^^
公共类DbListBase:ObservableCollection
//这里^^^^
但是我不喜欢泛型类型根据泛型参数做不同事情的想法。泛型应该是类型独立的

因此,我建议为您需要的每种类型制作不同的衍生物:

public class DbListBaseForType1 : ObservableCollection<Type1> // Type1 as generic argument for the base class
{
    // special behaviour for Type1
}

public class DbListBaseForType2 : ObservableCollection<Type2> // Type2 as generic argument for the base class
{
    // special behaviour for Type1
}
公共类DbListBaseForType1:ObservableCollection//Type1作为基类的泛型参数
{
//类型1的特殊行为
}
公共类DbListBaseForType2:ObservableCollection//Type2作为基类的泛型参数
{
//类型1的特殊行为
}

谢谢,这很好用。其思想是用包含数据库值的对象填充集合。根据对象的类型,我必须访问不同的databasevieww。
public class DbListBase<T> : ObservableCollection<T>
               // here ^^^^
public class DbListBaseForType1 : ObservableCollection<Type1> // Type1 as generic argument for the base class
{
    // special behaviour for Type1
}

public class DbListBaseForType2 : ObservableCollection<Type2> // Type2 as generic argument for the base class
{
    // special behaviour for Type1
}