Exemple #1
0
        // Returns the IEnumerable interface implemented by the given type,
        // preferring System.Collections.Generic.IEnumerable<T> over
        // System.Collections.IEnumerable. If there are multiple implementations
        // of IEnumerable<T> on base and derived types, the implementation on
        // the most derived type is returned. If there are multiple implementations
        // of IEnumerable<T> on the same type, it is undefined which is returned.
        internal static Type GetIEnumerableImplementationIfAny(this Type type)
        {
            var t = type;

            do
            {
                foreach (var @interface in t.GetInterfacesOnType())
                {
                    if (@interface.IsIEnumerableOfT())
                    {
                        // Return the first implementation of IEnumerable<T>.
                        return(@interface);
                    }
                }
                t = t.BaseType;
            } while (t != null);

            foreach (var @interface in type.GetInterfaces())
            {
                if (@interface.IsIEnumerable())
                {
                    return(@interface);
                }
            }

            return(null);
        }
Exemple #2
0
        // Returns the IEnumerable interface implemented by the given type,
        // preferring System.Collections.Generic.IEnumerable<T> over
        // System.Collections.IEnumerable. If there are multiple implementations
        // of IEnumerable<T> on base and derived types, the implementation on
        // the most derived type is returned. If there are multiple implementations
        // of IEnumerable<T> on the same type, it is undefined which is returned.
        internal static Type GetIEnumerableImplementationIfAny(this Type type)
        {
            var t = type;

            do
            {
                foreach (var @interface in t.GetInterfacesOnType())
                {
                    if (@interface.IsMscorlibType("System.Collections.Generic", "IEnumerable`1"))
                    {
                        // Return the first implementation of IEnumerable<T>.
                        return(@interface);
                    }
                }
                t = t.BaseType;
            } while (t != null);

            foreach (var @interface in type.GetInterfaces())
            {
                if (@interface.IsMscorlibType("System.Collections", "IEnumerable"))
                {
                    return(@interface);
                }
            }

            return(null);
        }
Exemple #3
0
 internal static bool IsIDynamicMetaObjectProvider(this Type type)
 {
     foreach (var @interface in type.GetInterfaces())
     {
         if (@interface.IsType("System.Dynamic", "IDynamicMetaObjectProvider"))
         {
             return(true);
         }
     }
     return(false);
 }
Exemple #4
0
        private static Type GetAncestorType(Type type, string ancestorTypeName)
        {
            if (type.FullName == ancestorTypeName)
            {
                return(type);
            }
            // Search interfaces.
            foreach (var @interface in type.GetInterfaces())
            {
                var ancestorType = GetAncestorType(@interface, ancestorTypeName);
                if (ancestorType != null)
                {
                    return(ancestorType);
                }
            }
            // Search base type.
            var baseType = type.BaseType;

            if (baseType != null)
            {
                return(GetAncestorType(baseType, ancestorTypeName));
            }
            return(null);
        }
Exemple #5
0
 private static Type GetAncestorType(Type type, string ancestorTypeName)
 {
     if (type.FullName == ancestorTypeName)
     {
         return type;
     }
     // Search interfaces.
     foreach (var @interface in type.GetInterfaces())
     {
         var ancestorType = GetAncestorType(@interface, ancestorTypeName);
         if (ancestorType != null)
         {
             return ancestorType;
         }
     }
     // Search base type.
     var baseType = type.BaseType;
     if (baseType != null)
     {
         return GetAncestorType(baseType, ancestorTypeName);
     }
     return null;
 }