Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/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# 创建自己的集合类型的“类型”对象;“类型”;在C中#_C#_Collections - Fatal编程技术网

C# 创建自己的集合类型的“类型”对象;“类型”;在C中#

C# 创建自己的集合类型的“类型”对象;“类型”;在C中#,c#,collections,C#,Collections,我是C#的新手,我有以下问题: 我需要创建一个TypeCollection,它继承自Collection,这里的对象类型是我创建的一些类型 在InsertItem()重载方法中,我想检查对象是否来自我创建的特定类型层次结构,否则抛出异常 代码片段已附加到: public class ObjectTypeCollection<Type> : Collection<Type> { protected override void InsertItem(int index

我是C#的新手,我有以下问题:

我需要创建一个TypeCollection,它继承自Collection,这里的对象类型是我创建的一些类型

在InsertItem()重载方法中,我想检查对象是否来自我创建的特定类型层次结构,否则抛出异常

代码片段已附加到:

public class ObjectTypeCollection<Type> : Collection<Type>
{
    protected override void InsertItem(int index, Type item)
    {   
        if(!(Utility.IsFDTObject(item.GetType())))
        {          
            throw new ArgumentException(string.Format("Type {0} is not valid", item.ToString()));
        }
        base.InsertItem(index, item);
    }
}
获取类型,然后将其传递给实用程序方法。这个很好用。这是正确的方法吗


您能在这里帮助我吗?

您可以对类型参数
类型设置约束,请参见此处:

这是静态检查的,您不需要像当前这样做任何动态操作。具体而言:

公共类ObjectTypeCollection:Collection,其中T:

可用于检查类型的实例是否可从另一类型的实例分配(如果它们兼容)。像这样:

if (typeof(FDTObject).IsAssignableFrom(item))
但你的问题有点不清楚。也许您不希望插入实际的类型,而是希望插入特定类型的对象,并且能够用不同的类型实例化集合?然后可以在类中约束泛型参数:

public class ObjectTypeCollection<T> : Collection<T> where T: FDTObject
公共类ObjectTypeCollection:集合,其中T:FDTObject
或者您只需要一个集合,其中所有对象都是一个对象或其后代。然后,您可以使用
列表
,进行即时静态类型检查(如果这是您想要的,那么这是最好的解决方案):

List=newlist();
但对我来说,这还很不清楚。是否要将
System.Type
的实例添加到集合中(然后需要直接删除类名后的第一个泛型参数)?或者您是否碰巧选择了
Type
作为泛型参数的名称(这是一个错误的选择,因为已经有一个类型,即
System.Type
这样命名)?

使用方法:

public class FDTObject {}
public class MyDTObject1 : FDTObject {}
public class MyDTObject2 : FDTObject { }

public class ObjectTypeCollection : Collection<Type>
{
    protected override void InsertItem(int index, Type item)
    {
        if (!typeof(FDTObject).IsAssignableFrom(item))
        {
            throw new ArgumentException(string.Format("Type {0} is not valid", item));
        }
        base.InsertItem(index, item);
    }
}

除非我误解了你想要什么,你就不能用这个吗

可以使用设置为基类的类型参数初始化列表:

var list = new List<FDTObject>(); // assuming this is one of your base classes based upon your example.
var list=new list();//假设这是一个基于示例的基类。

然后,您可以将任何对象添加到列表中,该对象是
FDTObject
或继承自
FDTObject

GetType
获取项目的类型。你所说的“我已通过的类型”是什么意思?你使用
type
作为泛型参数名,完全把我搞糊涂了。上次的回答解决了我的问题。我使用IsAssignableFrom()来实现需求。我使用Type,因为它应该是一个类型集合。@user1064490然后我建议您删除类名
ObjectTypeCollection
之后的
。因为目前,
Type
这个词并没有按照您的意思使用(
System.Type
),而是作为一个通用类型参数名,即用户也可以实例化一个
ObjectTypeCollection
,这可能不是您想要的。我想,topic starter想要一个System.Type的集合,哪些成员是从某些基类型派生的。谢谢!可分配的工作。我以前用过,但我用的是另一种方式。item.IsAssignableFrom(typeof(FDTObject))显然是错误的。。。
public class FDTObject {}
public class MyDTObject1 : FDTObject {}
public class MyDTObject2 : FDTObject { }

public class ObjectTypeCollection : Collection<Type>
{
    protected override void InsertItem(int index, Type item)
    {
        if (!typeof(FDTObject).IsAssignableFrom(item))
        {
            throw new ArgumentException(string.Format("Type {0} is not valid", item));
        }
        base.InsertItem(index, item);
    }
}
var collection = new ObjectTypeCollection();  
collection.Add(typeof(MyDTObject1)); // ok  
collection.Add(typeof(MyDTObject2)); // ok  
collection.Add(typeof(String)); // throws an exception  
var list = new List<FDTObject>(); // assuming this is one of your base classes based upon your example.