声明BaseType的ObservableCollection <T>并创建ChildType的实例

时间:2019-06-22 13:18:11

标签: c#

我想执行以下操作,但是我不知道如何声明ObservableCollection

class SomeClass
{

    SomeClass(PropertyInfo prop)
    {
        Type detailTyp = prop.GetType();

        ObservableCollection<???> col;
        //ObservableCollection<detailTyp> col; doesn't work, 'detailTyp' is a variable but used like a Type

        if (detailTyp.Name == "a")
        {//detailTyp can only return Types that inherit from a certain base class
            col = new ObservableCollection<a>();
            //If I use ObservableCollection<baseClass>, then I get a conversion error here

        }
        else if (detailTyp.Name == "b")
        {
            col = new ObservableCollection<b>();
        }

        foreach (DataRow row in table.Rows)
        {
            object o = Activator.CreateInstance(detailTyp());

            col.Add(o);
        }
    }
}   

0 个答案:

没有答案