zoukankan      html  css  js  c++  java
  • How do I check if a type is a subtype OR the type of an object?

    How do I check if a type is a subtype OR the type of an object?

    To check if a type is a subclass of another type in C#, it's easy:

    typeof (SubClass).IsSubclassOf(typeof (BaseClass)); // returns true

    However, this will fail:

    typeof (BaseClass).IsSubclassOf(typeof (BaseClass)); // returns false

    Is there any way to check whether a type is either a subclass OR of the base class itself, without using an OR operator or using an extension method?

    Apparently, no.

    Here's the options:

    Type.IsSubclassOf

    As you've already found out, this will not work if the two types are the same, here's a sample LINQPad program that demonstrates:

    void Main()
    {
        typeof(Derived).IsSubclassOf(typeof(Base)).Dump();
        typeof(Base).IsSubclassOf(typeof(Base)).Dump();
    }
    
    public class Base { }
    public class Derived : Base { }

    Output:

    True
    False

    Which indicates that Derived is a subclass of Base, but that Baseis (obviously) not a subclass of itself.

    Type.IsAssignableFrom

    Now, this will answer your particular question, but it will also give you false positives. As Eric Lippert has pointed out in the comments, while the method will indeed return True for the two above questions, it will also return True for these, which you probably don't want:

    void Main()
    {
        typeof(Base).IsAssignableFrom(typeof(Derived)).Dump();
        typeof(Base).IsAssignableFrom(typeof(Base)).Dump();
        typeof(int[]).IsAssignableFrom(typeof(uint[])).Dump();
    }
    
    public class Base { }
    public class Derived : Base { }

    Here you get the following output:

    True
    True
    True

    The last True there would indicate, if the method only answered the question asked, that uint[] inherits from int[] or that they're the same type, which clearly is not the case.

    So IsAssignableFrom is not entirely correct either.

    is and as

    The "problem" with is and as in the context of your question is that they will require you to operate on the objects and write one of the types directly in code, and not work with Type objects.

    In other words, this won't compile:

    SubClass is BaseClass
    ^--+---^
       |
       +-- need object reference here

    nor will this:

    typeof(SubClass) is typeof(BaseClass)
                        ^-------+-------^
                                |
                                +-- need type name here, not Type object

    nor will this:

    typeof(SubClass) is BaseClass
    ^------+-------^
           |
           +-- this returns a Type object, And "System.Type" does not
               inherit from BaseClass

    Conclusion

    While the above methods might fit your needs, the only correct answer to your question (as I see it) is that you will need an extra check:

    typeof(Derived).IsSubclassOf(typeof(Base)) || typeof(Derived) == typeof(Base);

    which of course makes more sense in a method:

    public bool IsSameOrSubclass(Type potentialBase, Type potentialDescendant)
    {
        return potentialDescendant.IsSubclassOf(potentialBase)
               || potentialDescendant == potentialBase;
    }
    Note that this doesn't actually do what the question asked for; this does not determine whether one type is a subclass of another, but rather whether one type is assignment compatible with another. An array of uint isn't a subclass of an array of int, but they are assignment compatible. IEnumerable<Giraffe> isn't a subclass of IEnumerable<Animal>, but they are assignment compatible in v4. – Eric Lippert Apr 30 '10 at 6:07

    测试

     class A
        {
    
        }
    
        class B : A
        {
        }
    
        class C : B
        {
        }
    
     [Test]
            public void SubClassTest()
            {
                Console.WriteLine(typeof(A).IsSubclassOf(typeof(A)));
                Console.WriteLine(typeof(B).IsSubclassOf(typeof(A)));
                Console.WriteLine(typeof(C).IsSubclassOf(typeof(A))); 
            }

    输出结果是

    False
    True
    True

  • 相关阅读:
    poj 2942 Knights of the Round Table(无向图的双连通分量+二分图判定)
    Java序列化的几种方式
    Android 四大组件学习之Service六
    cursor:pointer的意思
    JSP中<base href="<%=basePath%>">的作用
    一篇让Java程序猿随时可以翻看的Oracle总结
    史上最全的javascript知识点总结,浅显易懂。
    史上最全的HTML、CSS知识点总结,浅显易懂。
    css学习归纳总结
    Web前端开发Chrome插件
  • 原文地址:https://www.cnblogs.com/chucklu/p/10636500.html
Copyright © 2011-2022 走看看