Exemplo n.º 1
0
        internal static Type GetListItemType(TypeModel model, Type listType)
        {
            if (((listType != model.MapType(typeof(string))) && !listType.IsArray) && model.MapType(typeof(IEnumerable)).IsAssignableFrom(listType))
            {
                BasicList candidates = new BasicList();
                foreach (MethodInfo info in listType.GetMethods())
                {
                    if (!info.IsStatic && !(info.Name != "Add"))
                    {
                        Type            type;
                        ParameterInfo[] parameters = info.GetParameters();
                        if ((parameters.Length == 1) && !candidates.Contains(type = parameters[0].ParameterType))
                        {
                            candidates.Add(type);
                        }
                    }
                }
                string name = listType.Name;
                if ((name == null) || ((name.IndexOf("Queue") < 0) && (name.IndexOf("Stack") < 0)))
                {
                    TestEnumerableListPatterns(model, candidates, listType);
                    foreach (Type type2 in listType.GetInterfaces())
                    {
                        TestEnumerableListPatterns(model, candidates, type2);
                    }
                }
                foreach (PropertyInfo info2 in listType.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))
                {
                    if (!(info2.Name != "Item") && !candidates.Contains(info2.PropertyType))
                    {
                        ParameterInfo[] indexParameters = info2.GetIndexParameters();
                        if ((indexParameters.Length == 1) && !(indexParameters[0].ParameterType != model.MapType(typeof(int))))
                        {
                            candidates.Add(info2.PropertyType);
                        }
                    }
                }
                switch (candidates.Count)
                {
                case 0:
                    return(null);

                case 1:
                    return((Type)candidates[0]);

                case 2:
                    if (!CheckDictionaryAccessors(model, (Type)candidates[0], (Type)candidates[1]))
                    {
                        if (CheckDictionaryAccessors(model, (Type)candidates[1], (Type)candidates[0]))
                        {
                            return((Type)candidates[1]);
                        }
                        break;
                    }
                    return((Type)candidates[0]);
                }
            }
            return(null);
        }
Exemplo n.º 2
0
 private static void TestEnumerableListPatterns(TypeModel model, BasicList candidates, Type iType)
 {
     if (iType.IsGenericType)
     {
         Type genericTypeDefinition = iType.GetGenericTypeDefinition();
         if (((genericTypeDefinition == model.MapType(typeof(IEnumerable <>))) || (genericTypeDefinition == model.MapType(typeof(ICollection <>)))) || (genericTypeDefinition.FullName == "System.Collections.Concurrent.IProducerConsumerCollection`1"))
         {
             Type[] genericArguments = iType.GetGenericArguments();
             if (!candidates.Contains(genericArguments[0]))
             {
                 candidates.Add(genericArguments[0]);
             }
         }
     }
 }