CreateGenericArrayItem() публичный статический Метод

Creates an array item based on the data type of the parent.
public static CreateGenericArrayItem ( QbItemBase parent ) : GenericQbItem
parent QbItemBase Must be a simple array type
Результат GenericQbItem
Пример #1
0
        public static void SetGenericItems(QbItemBase item, List <GenericQbItem> gItems)
        {
            MemberInfo[] ms = item.GetType().FindMembers(MemberTypes.Property,
                                                         /*BindingFlags.DeclaredOnly |*/ BindingFlags.Instance | BindingFlags.Public,
                                                         Type.FilterName, "*");

            int                  i = 0;
            GenericQbItem        gi;
            List <GenericQbItem> list = null;

            if (gItems.Count == 0)
            {
                //test if this is an array item
                GenericQbItem gqi = QbFile.CreateGenericArrayItem(item);

                //null if not an array type
                if (gqi != null)
                {
                    //use this item to identify the array to set to 0 items
                    MemberInfo m = Array.Find(ms, delegate(MemberInfo mi)
                    {
                        return(mi.Name == gqi.SourceProperty);
                    });
                    if (m != null)
                    {
                        Type t = ((PropertyInfo)m).GetValue(item, null).GetType();
                        if (t == typeof(QbKey[]))
                        {
                            ((PropertyInfo)m).SetValue(item, new QbKey[0], null);
                        }
                        else if (t == typeof(float[]))
                        {
                            ((PropertyInfo)m).SetValue(item, new float[0], null);
                        }
                        else if (t == typeof(uint[]))
                        {
                            ((PropertyInfo)m).SetValue(item, new uint[0], null);
                        }
                        else if (t == typeof(int[]))
                        {
                            ((PropertyInfo)m).SetValue(item, new int[0], null);
                        }
                        else if (t == typeof(string[]))
                        {
                            ((PropertyInfo)m).SetValue(item, new string[0], null);
                        }
                    }
                }
            }


            while (i < gItems.Count)
            {
                gi = gItems[i++];

                if (gi.ReadOnly)
                {
                    continue;
                }

                //list = null;

                list = new List <GenericQbItem>();
                list.Add(gi);
                while (i < gItems.Count && gi.SourceProperty == gItems[i].SourceProperty)
                {
                    list.Add(gItems[i++]);
                }

                MemberInfo m = Array.Find(ms, delegate(MemberInfo mi)
                {
                    return(mi.Name == gi.SourceProperty);
                });

                if (m != null)
                {
                    Type t = ((PropertyInfo)m).GetValue(item, null).GetType();
                    if (t == typeof(QbKey[]))
                    {
                        QbKey[] qb = new QbKey[list.Count];
                        QbKey   q;
                        string  qS;

                        for (int c = 0; c < list.Count; c++)
                        {
                            q  = list[c].ToQbKey();
                            qS = item.Root.LookupDebugName(q.Crc);
                            if (qS.Length != 0)
                            {
                                q = QbKey.Create(q.Crc, qS);
                            }
                            qb[c] = q;
                        }
                        ((PropertyInfo)m).SetValue(item, qb, null);
                    }
                    else if (t == typeof(float[]))
                    {
                        float[] f = new float[list.Count];
                        for (int c = 0; c < list.Count; c++)
                        {
                            f[c] = list[c].ToSingle();
                        }
                        ((PropertyInfo)m).SetValue(item, f, null);
                    }
                    else if (t == typeof(uint[]))
                    {
                        uint[] ui = new uint[list.Count];
                        for (int c = 0; c < list.Count; c++)
                        {
                            ui[c] = list[c].ToUInt32();
                        }
                        ((PropertyInfo)m).SetValue(item, ui, null);
                    }
                    else if (t == typeof(int[]))
                    {
                        int[] si = new int[list.Count];
                        for (int c = 0; c < list.Count; c++)
                        {
                            si[c] = list[c].ToInt32();
                        }
                        ((PropertyInfo)m).SetValue(item, si, null);
                    }
                    else if (t == typeof(string[]))
                    {
                        string[] s = new string[list.Count];
                        for (int c = 0; c < list.Count; c++)
                        {
                            s[c] = list[c].ToString();
                        }
                        ((PropertyInfo)m).SetValue(item, s, null);
                    }
                    else if (t == typeof(QbKey))
                    {
                        QbKey  q  = gi.ToQbKey();
                        string qS = item.Root.LookupDebugName(q.Crc);
                        if (qS.Length != 0)
                        {
                            q = QbKey.Create(q.Crc, qS);
                        }
                        ((PropertyInfo)m).SetValue(item, q, null);
                    }
                    else if (t == typeof(float))
                    {
                        ((PropertyInfo)m).SetValue(item, gi.ToSingle(), null);
                    }
                    else if (t == typeof(uint))
                    {
                        ((PropertyInfo)m).SetValue(item, gi.ToUInt32(), null);
                    }
                    else if (t == typeof(int))
                    {
                        ((PropertyInfo)m).SetValue(item, gi.ToInt32(), null);
                    }
                    else if (t == typeof(string))
                    {
                        ((PropertyInfo)m).SetValue(item, gi.ToString(), null);
                    }
                    else if (t == typeof(byte[]))
                    {
                        ((PropertyInfo)m).SetValue(item, gi.ToByteArray(), null);
                    }
                    else
                    {
                        throw new ApplicationException(string.Format("DataType {0} not supported.", t.Name));
                    }
                }
            }
        }