C#.NET - How can I get typeof() to work with inheritance?

后端 未结 6 2163
醉话见心
醉话见心 2021-02-18 22:47

I will start by explaining my scenario in code:

public class A { }

public class B : A { }

public class C : B { }

public class D { }

public c         


        
6条回答
  •  有刺的猬
    2021-02-18 23:03

    typeof(B).IsInstanceOfType(c)
    

    Similar to the answer above from sam-harwell, sometimes you may have the type "B" in a variable, so you need to use reflection rather than the "is" operator.

    I used Sam's solution, and was pleasantly surprised when Resharper made this suggestion.

提交回复
热议问题