Exemple #1
0
        public static bool OpenGenericIsAssignableFrom(
            TypeIdentity openGeneric,
            TypeDefinition type,
            Func <TypeIdentity, TypeDefinition> toDefinition)
        {
            // Terminate recursion
            if ((type == null) || openGeneric.Equals(type))
            {
                return(false);
            }

            // typeToCheck is a closure of openGenericType
            var isClosureOfGenericType = type.Identity.IsGenericType && openGeneric.Equals(type.GenericTypeDefinition);

            // typeToCheck is the subclass of a closure of openGenericType
            var isSubClassOfClosure = OpenGenericIsAssignableFrom(openGeneric, toDefinition(type.BaseType), toDefinition);

            // typeToCheck inherits from an interface which is the closure of openGenericType
            var inheritsClosureInterface = type.BaseInterfaces.Any(
                interfaceType => OpenGenericIsAssignableFrom(openGeneric, toDefinition(interfaceType), toDefinition));

            return(isClosureOfGenericType || isSubClassOfClosure || inheritsClosureInterface);
        }