Пример #1
0
        private static IFirestoreInternalConverter CreateConverter(BclType targetType)
        {
            var targetTypeInfo = targetType.GetTypeInfo();

            if (targetType.IsArray)
            {
                return(new ArrayConverter(targetType.GetElementType()));
            }
            if (targetTypeInfo.IsDefined(typeof(FirestoreDataAttribute)))
            {
                return(AttributedTypeConverter.ForType(targetType));
            }
            // Simple way of checking for an anonymous type. Far from foolproof, but a reasonable start.
            if (targetTypeInfo.IsDefined(typeof(CompilerGeneratedAttribute)))
            {
                return(new AnonymousTypeConverter(targetType));
            }

            if (targetTypeInfo.IsEnum)
            {
                return(new EnumConverter(targetType));
            }
            if (TryGetStringDictionaryValueType(targetType) is BclType dictionaryElementType)
            {
                var method = s_createDictionaryConverter.MakeGenericMethod(dictionaryElementType);
                try
                {
                    return((IFirestoreInternalConverter)method.Invoke(null, new object[] { targetType }));
                }
                catch (TargetInvocationException e) when(e.InnerException != null)
                {
                    throw e.InnerException;
                }
            }

            // This will only return true if the target type can be assigned
            // from List<T>, and the target type implements IEnumerable<T>. That's
            // enough to make ListConverter work: when serializing, it will just use
            // IEnumerable, and when deserializing it will construct a List<T>.
            if (TryGetListType(targetType) is BclType listType)
            {
                return(new ListConverter(listType));
            }

            if (typeof(IList).IsAssignableFrom(targetType))
            {
                return(new ListConverter(targetType));
            }

            throw new ArgumentException($"Unable to create converter for type {targetType.GetTypeInfo().FullName}");
        }
Пример #2
0
        private static IFirestoreInternalConverter CreateConverter(BclType targetType)
        {
            var targetTypeInfo = targetType.GetTypeInfo();

            if (targetType.IsArray)
            {
                return(new ArrayConverter(targetType.GetElementType()));
            }
            if (targetTypeInfo.IsDefined(typeof(FirestoreDataAttribute)))
            {
                return(AttributedTypeConverter.ForType(targetType));
            }
            // Simple way of checking for an anonymous type. Far from foolproof, but a reasonable start.
            if (targetTypeInfo.IsDefined(typeof(CompilerGeneratedAttribute)))
            {
                return(new AnonymousTypeConverter(targetType));
            }

            if (targetTypeInfo.IsEnum)
            {
                return(new EnumConverter(targetType));
            }
            if (TryGetStringDictionaryValueType(targetType, out var dictionaryElementType))
            {
                var method = s_createDictionaryConverter.MakeGenericMethod(dictionaryElementType);
                try
                {
                    return((IFirestoreInternalConverter)method.Invoke(null, new object[] { targetType }));
                }
                catch (TargetInvocationException e) when(e.InnerException != null)
                {
                    throw e.InnerException;
                }
            }

            if (typeof(IList).IsAssignableFrom(targetType))
            {
                return(new ListConverter(targetType));
            }

            throw new ArgumentException($"Unable to create converter for type {targetType.GetTypeInfo().FullName}");
        }