Ejemplo n.º 1
0
        internal static BasicList GetContiguousGroups(int[] keys, object[] values)
        {
            if (keys == null)
            {
                throw new ArgumentNullException("keys");
            }
            if (values == null)
            {
                throw new ArgumentNullException("values");
            }
            if (values.Length < keys.Length)
            {
                throw new ArgumentException("Not all keys are covered by values", "values");
            }
            BasicList list  = new BasicList();
            Group     group = null;

            for (int i = 0; i < keys.Length; i++)
            {
                if ((i == 0) || (keys[i] != keys[i - 1]))
                {
                    group = null;
                }
                if (group == null)
                {
                    group = new Group(keys[i]);
                    list.Add(group);
                }
                group.Items.Add(values[i]);
            }
            return(list);
        }
Ejemplo n.º 2
0
        private bool TryDeserializeList(TypeModel model, ProtoReader reader, DataFormat format, int tag, Type listType, Type itemType, ref object value)
        {
            bool       flag;
            MethodInfo info = ResolveListAdd(model, listType, itemType, out flag);

            if (info == null)
            {
                throw new NotSupportedException("Unknown list variant: " + listType.FullName);
            }
            bool   flag2 = false;
            object obj2  = null;
            IList  list  = value as IList;

            object[]  parameters = flag ? null : new object[1];
            BasicList list2      = listType.IsArray ? new BasicList() : null;

            while (this.TryDeserializeAuxiliaryType(reader, format, tag, itemType, ref obj2, true, true, true, true))
            {
                flag2 = true;
                if ((value == null) && (list2 == null))
                {
                    value = CreateListInstance(listType, itemType);
                    list  = value as IList;
                }
                if (list != null)
                {
                    list.Add(obj2);
                }
                else if (list2 != null)
                {
                    list2.Add(obj2);
                }
                else
                {
                    parameters[0] = obj2;
                    info.Invoke(value, parameters);
                }
                obj2 = null;
            }
            if (list2 != null)
            {
                Array array;
                if (value != null)
                {
                    if (list2.Count != 0)
                    {
                        Array sourceArray = (Array)value;
                        array = Array.CreateInstance(itemType, (int)(sourceArray.Length + list2.Count));
                        Array.Copy(sourceArray, array, sourceArray.Length);
                        list2.CopyTo(array, sourceArray.Length);
                        value = array;
                    }
                    return(flag2);
                }
                array = Array.CreateInstance(itemType, list2.Count);
                list2.CopyTo(array, 0);
                value = array;
            }
            return(flag2);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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]);
             }
         }
     }
 }
Ejemplo n.º 5
0
 public Group(int first)
 {
     this.First = first;
     this.Items = new BasicList();
 }