识别 C# IList 中对象的类

发布于 2024-10-28 08:51:31 字数 227 浏览 3 评论 0原文

我有一个 IList 变量,其中包含 1 - N 个 myObject 条目。每个条目可以是 myObject 或其任何子对象(派生类对象)的实例。在对 IList 执行 foreach 时,如何识别 IList 的每个成员属于哪种类型的对象?

foreach(myObject anObject in myList)
{
   if(anObject is of type ???)
}

I have an IList variable which contains 1 - N entries of myObject. Each entry can be an instance of myObject or any of it's child objects (derived class objects). When doing a foreach over the IList, how can I identify which type of object each member of the IList is?

foreach(myObject anObject in myList)
{
   if(anObject is of type ???)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

魔法唧唧 2024-11-04 08:51:31

看一下is 运算符

foreach(MyObject anObject in myList)
{
   if(anObject is MyTypeWhichInheritsFromMyObject)
     ...
}

Take a look at the is operator.

foreach(MyObject anObject in myList)
{
   if(anObject is MyTypeWhichInheritsFromMyObject)
     ...
}
单挑你×的.吻 2024-11-04 08:51:31

您可以使用

anObject is MyObject

或者

Type.IsSubclassOf(MyObject)

也可以使用 baseType.IsAssignableFrom(type) 来确定类型是否可以从给定的基类型派生

You could make use of

anObject is MyObject

or

Type.IsSubclassOf(MyObject)

You can also make use of baseType.IsAssignableFrom(type) to determine if a type can be derived from a given base type

木落 2024-11-04 08:51:31

我不是一个 .NET 开发人员,但在 Java 中,有一个名为 instanceof 的运算符,它将检查对象是否是某个类的实例。

查看此链接,它与对象的 typeid 有关,我认为这就是您正在寻找的伙伴。

http://msdn.microsoft.com/en-us/库/b2ay8610(vs.71).aspx

I'm not much of a .NET developer but in Java there's an operator called instanceof that will check if the object is an instance of a certain class.

Check this link out, it has to do with the typeid of an object, I think that's what you're looking for mate.

http://msdn.microsoft.com/en-us/library/b2ay8610(vs.71).aspx

探春 2024-11-04 08:51:31

如果您想询问每个实例的类型,请这样做:

foreach(myObject anObject in myList)
{
    Type anObjectType = anObject.GetType();
    // Perform appropriate work.
}

If you want to ask for the type of each instance do it like this:

foreach(myObject anObject in myList)
{
    Type anObjectType = anObject.GetType();
    // Perform appropriate work.
}
你丑哭了我 2024-11-04 08:51:31

如果您的 OO 结构设计正确,您确实不需要知道 - 无论您想对对象做什么,都可以使用适当的实现来完成,无论它是什么类型。

话虽如此,您可以通过调用来获取任何对象的类型:

Type type = myObject.GetType();

您可以将该类型与另一个特定类型进行比较,例如:

if (myObject.GetType() == typeof(Foo))

is 运算符可能会给您带来麻烦,具体取决于您的情况,因为如果您正在检查某个东西是否是父类,即使它确实是一个子类,这也是正确的。例如:

class Foo {}
class Bar : Foo {}

if (myObject is Foo)

if 将为 FooBar 对象返回 true。

If your OO structure is designed properly, you really shouldn't need to know - whatever you want to do with the object can be done using the appropriate implementation, regardless of which type it is.

With that said, you can get the type of any object by calling:

Type type = myObject.GetType();

And you can compare that type to another specific type, like:

if (myObject.GetType() == typeof(Foo))

The is operator can get you in trouble, depending on your scenario, since it will be true if you are checking if something is a parent class, even if it really is a child. For example:

class Foo {}
class Bar : Foo {}

if (myObject is Foo)

This if will return true for either Foo or Bar objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文