Check if a class is derived from a generic class

前端 未结 16 1660
太阳男子
太阳男子 2020-11-22 09:57

I have a generic class in my project with derived classes.

public class GenericClass : GenericInterface
{
}

public class Test : GenericCla         


        
16条回答
  •  生来不讨喜
    2020-11-22 10:04

    late to the game on this... i too have yet another permutation of JarodPar's answer.

    here's Type.IsSubClassOf(Type) courtesy of reflector:

        public virtual bool IsSubclassOf(Type c)
        {
            Type baseType = this;
            if (!(baseType == c))
            {
                while (baseType != null)
                {
                    if (baseType == c)
                    {
                        return true;
                    }
                    baseType = baseType.BaseType;
                }
                return false;
            }
            return false;
        }
    

    from that, we see that it's not doing anything too cray cray and is similar to JaredPar's iterative approach. so far, so good. here's my version (disclaimer: not thoroughly tested, so lemme know if you find issues)

        public static bool IsExtension(this Type thisType, Type potentialSuperType)
        {
            //
            // protect ya neck
            //
            if (thisType == null || potentialSuperType == null || thisType == potentialSuperType) return false;
    
            //
            // don't need to traverse inheritance for interface extension, so check/do these first
            //
            if (potentialSuperType.IsInterface)
            {
                foreach (var interfaceType in thisType.GetInterfaces())
                {
                    var tempType = interfaceType.IsGenericType ? interfaceType.GetGenericTypeDefinition() : interfaceType;
    
                    if (tempType == potentialSuperType)
                    {
                        return true;
                    }
                }
            }
    
            //
            // do the concrete type checks, iterating up the inheritance chain, as in orignal
            //
            while (thisType != null && thisType != typeof(object))
            {
                var cur = thisType.IsGenericType ? thisType.GetGenericTypeDefinition() : thisType;
    
                if (potentialSuperType == cur)
                {
                    return true;
                }
    
                thisType = thisType.BaseType;
            }
            return false;
        }
    

    basically this is just an extension method to System.Type - i did this to intentionally limit the "thisType" Type to concrete Types, as my immediate usage is to LINQ query "where" predicates against Type objects. i'm sure all you smart folks out there could bang it down to an efficient, all-purpose static method if you need to :) the code does a few things the answer's code doesn't

    1. open's it up to to general "extension" - i'm considering inheritance (think classes) as well as implementation (interfaces); method and parameter names are changed to better reflect this
    2. input null-validation (meah)
    3. input of same type (a class cannot extend itself)
    4. short-circuit execution if Type in question is an interface; because GetInterfaces() returns all implemented interfaces (even ones implemented in super-classes), you can simply loop through that collection not having to climb the inheritance tree

    the rest is basically the same as JaredPar's code

提交回复
热议问题