Exemplo n.º 1
0
        internal bool ResolveMapTypes(out Type dictionaryType, out Type keyType, out Type valueType)
        {
            dictionaryType = keyType = valueType = null;
            try
            {
#if COREFX || PROFILE259
                var info = MemberType.GetTypeInfo();
#else
                var info = MemberType;
#endif
                if (ImmutableCollectionDecorator.IdentifyImmutable(MemberType, out _, out _, out _, out _, out _, out _))
                {
                    return(false);
                }
                if (info.IsInterface && info.IsGenericType && info.GetGenericTypeDefinition() == typeof(IDictionary <,>))
                {
#if PROFILE259
                    var typeArgs = MemberType.GetGenericTypeDefinition().GenericTypeArguments;
#else
                    var typeArgs = MemberType.GetGenericArguments();
#endif
                    if (IsValidMapKeyType(typeArgs[0]))
                    {
                        keyType        = typeArgs[0];
                        valueType      = typeArgs[1];
                        dictionaryType = MemberType;
                    }
                    return(false);
                }
#if PROFILE259
                foreach (var iType in MemberType.GetTypeInfo().ImplementedInterfaces)
#else
                foreach (var iType in MemberType.GetInterfaces())
#endif
                {
#if COREFX || PROFILE259
                    info = iType.GetTypeInfo();
#else
                    info = iType;
#endif

                    if (info.IsGenericType && info.GetGenericTypeDefinition() == typeof(IDictionary <,>))
                    {
                        if (dictionaryType != null)
                        {
                            throw new InvalidOperationException("Multiple dictionary interfaces implemented by type: " + MemberType.FullName);
                        }
#if PROFILE259
                        var typeArgs = iType.GetGenericTypeDefinition().GenericTypeArguments;
#else
                        var typeArgs = iType.GetGenericArguments();
#endif
                        if (IsValidMapKeyType(typeArgs[0]))
                        {
                            keyType        = typeArgs[0];
                            valueType      = typeArgs[1];
                            dictionaryType = MemberType;
                        }
                    }
                }
                if (dictionaryType == null)
                {
                    return(false);
                }

                // (note we checked the key type already)
                // not a map if value is repeated
                Type itemType = null, defaultType = null;
                model.ResolveListTypes(valueType, ref itemType, ref defaultType);
                if (itemType != null)
                {
                    return(false);
                }

                return(dictionaryType != null);
            }
            catch
            {
                // if it isn't a good fit; don't use "map"
                return(false);
            }
        }
Exemplo n.º 2
0
 public Type GetValueType()
 {
     return(MemberType.GetInterfaces().Single(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IGetSet <>)).GetGenericArguments()[0]);
 }