Пример #1
0
 private static bool FindInType(
     IReadOnlyObjectDataConverterCollection collection, Type type, out ObjectDataConverter converter)
 {
     return(type.IsGenericType
         ? collection.TryGetConverter(type.GetGenericTypeDefinition(), out converter)
         : collection.TryGetConverter(type, out converter));
 }
Пример #2
0
        public static bool TryGetConverter(
            this IReadOnlyObjectDataConverterCollection self, Type convertType, out ObjectDataConverter converter)
        {
            if (!self.TryGetConverters(convertType, out var converters) || converters.Count == 0)
            {
                converter = null;
                return(false);
            }

            converter = converters.First();
            return(true);
        }
Пример #3
0
        private static bool FindInBaseTypes(
            IReadOnlyObjectDataConverterCollection collection, Type type, out ObjectDataConverter converter)
        {
            var p = type.BaseType;

            while (p != null)
            {
                if (FindInType(collection, p, out converter))
                {
                    return(true);
                }
                p = p.BaseType;
            }

            converter = null;
            return(false);
        }
Пример #4
0
        private static bool FindInInterfaces(
            IReadOnlyObjectDataConverterCollection collection, Type type, out ObjectDataConverter converter)
        {
            var interfaces = type.GetMostDerivedInterfaces();

            foreach (var @interface in interfaces)
            {
                if (FindInType(collection, @interface, out converter))
                {
                    return(true);
                }
            }

            foreach (var @interface in interfaces)
            {
                if (FindInInterfaces(collection, @interface, out converter))
                {
                    return(true);
                }
            }

            converter = null;
            return(false);
        }
Пример #5
0
        public virtual ObjectDataConverter Select(IReadOnlyObjectDataConverterCollection collection, Type convertType)
        {
            Ensure.Argument.NotNull(collection, nameof(collection));
            if (collection.Count == 0)
            {
                return(null);
            }

            ObjectDataConverter converter;

            if (FindInType(collection, convertType, out converter))
            {
                return(converter);
            }
            if (FindInInterfaces(collection, convertType, out converter))
            {
                return(converter);
            }
            if (FindInBaseTypes(collection, convertType, out converter))
            {
                return(converter);
            }
            return(null);
        }
Пример #6
0
 public static ObjectDataConverter GetConverter(
     this IReadOnlyObjectDataConverterCollection self, Type convertType)
 {
     return(self[convertType].FirstOrDefault());
 }