Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.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#_Inheritance - Fatal编程技术网

C# 获取子类类型

C# 获取子类类型,c#,inheritance,C#,Inheritance,我有三门课: class O { } class A : O { } class B : A { } 当我调用我的代码时: List<O> myList = new List<O>(); myList.Add(new A()); myList.Add(new B()); foreach (O obj in myList) { if (obj is A) { // do something } else if (ob

我有三门课:

class O
{
}

class A : O
{
}

class B : A
{
}
当我调用我的代码时:

List<O> myList = new List<O>();
myList.Add(new A());
myList.Add(new B());

foreach (O obj in myList)
{
    if (obj is A)
    {
         // do something
    }
    else if (obj is B)
    {
         //do something
    }
}
List myList=new List();
添加(新的A());
添加(新的B());
foreach(myList中的O obj)
{
if(obj是A)
{
//做点什么
}
否则如果(obj为B)
{
//做点什么
}
}

然而,我意识到,即使我的
obj
属于
B类
,if(obj是A)也将被评估为
true
。是否有一种编写语句的方法,使其在且仅当
obj
属于
B类时计算为true?

有两种方法
GetType
typeof

GetType是对象上的一个方法。它提供了一个类型对象,一个 指示对象实例的最派生类型

Typeof返回类型对象。它通常用作参数或参数 变量或字段。typeof运算符是表达式的一部分 获取类或值类型的类型指针

像这样试试

 if(obj.GetType() == typeof(A)) // do something
 else if(obj.GetType() == typeof(B)) //do something

有两种方法
GetType
typeof

GetType是对象上的一个方法。它提供了一个类型对象,一个 指示对象实例的最派生类型

Typeof返回类型对象。它通常用作参数或参数 变量或字段。typeof运算符是表达式的一部分 获取类或值类型的类型指针

像这样试试

 if(obj.GetType() == typeof(A)) // do something
 else if(obj.GetType() == typeof(B)) //do something

有两种方法
GetType
typeof

GetType是对象上的一个方法。它提供了一个类型对象,一个 指示对象实例的最派生类型

Typeof返回类型对象。它通常用作参数或参数 变量或字段。typeof运算符是表达式的一部分 获取类或值类型的类型指针

像这样试试

 if(obj.GetType() == typeof(A)) // do something
 else if(obj.GetType() == typeof(B)) //do something

有两种方法
GetType
typeof

GetType是对象上的一个方法。它提供了一个类型对象,一个 指示对象实例的最派生类型

Typeof返回类型对象。它通常用作参数或参数 变量或字段。typeof运算符是表达式的一部分 获取类或值类型的类型指针

像这样试试

 if(obj.GetType() == typeof(A)) // do something
 else if(obj.GetType() == typeof(B)) //do something

为什么不在基类中定义一个虚函数,并在派生类型中重写它,在不同的情况下执行所需的操作

class O {
    public virtual void DoSomething() {
        // do smtgh in the 'O' case
    }
}

class A : O {
    public override void DoSomething() {
        // do smtgh in the 'A' case
    }
}

class B : A {
    public override void DoSomething() {
        // do smtgh in the 'B' case
    }
}
然后你的循环就变成了

foreach (O obj in myList) {
    obj.DoSomething();
}

为什么不在基类中定义一个虚函数,并在派生类型中重写它,在不同的情况下执行所需的操作

class O {
    public virtual void DoSomething() {
        // do smtgh in the 'O' case
    }
}

class A : O {
    public override void DoSomething() {
        // do smtgh in the 'A' case
    }
}

class B : A {
    public override void DoSomething() {
        // do smtgh in the 'B' case
    }
}
然后你的循环就变成了

foreach (O obj in myList) {
    obj.DoSomething();
}

为什么不在基类中定义一个虚函数,并在派生类型中重写它,在不同的情况下执行所需的操作

class O {
    public virtual void DoSomething() {
        // do smtgh in the 'O' case
    }
}

class A : O {
    public override void DoSomething() {
        // do smtgh in the 'A' case
    }
}

class B : A {
    public override void DoSomething() {
        // do smtgh in the 'B' case
    }
}
然后你的循环就变成了

foreach (O obj in myList) {
    obj.DoSomething();
}

为什么不在基类中定义一个虚函数,并在派生类型中重写它,在不同的情况下执行所需的操作

class O {
    public virtual void DoSomething() {
        // do smtgh in the 'O' case
    }
}

class A : O {
    public override void DoSomething() {
        // do smtgh in the 'A' case
    }
}

class B : A {
    public override void DoSomething() {
        // do smtgh in the 'B' case
    }
}
然后你的循环就变成了

foreach (O obj in myList) {
    obj.DoSomething();
}

obj.GetType()==typeof(B)
只是一个思考练习,重新排列你的条件句。从最具体到最不具体。
obj.GetType()==typeof(B)
只是一个思考练习,重新排列你的条件句。从最具体到最不具体。
obj.GetType()==typeof(B)
只是一个思考练习,重新排列你的条件句。从最具体到最不具体。
obj.GetType()==typeof(B)
只是一个思考练习,重新排列你的条件句。从最具体到最不具体。