Exemplo n.º 1
0
        public static Dictionary <T1, ObservableCollection <T2> > GetGroupDicToList <T1, T2>(T1 key, ObservableCollection <T2> list)
        {
            Dictionary <T1, ObservableCollection <T2> > dic = new Dictionary <T1, ObservableCollection <T2> >();

            if (list == null || list.Count == 0)
            {
                return(dic);
            }

            MethodInfo method = typeof(T2).GetMethod("get_" + key.ToString());

            if (method == null)
            {
                throw new Exception("类" + typeof(T2).AssemblyQualifiedName + "没有这个方法:" + "get_" + key.ToString());
            }
            ObservableCollection <T2> outlist;

            foreach (T2 t2 in list)
            {
                object value = Utils.GetValueTrueType(method.Invoke(t2, null), typeof(T1).Name);
                if (value == null)
                {
                    System.Windows.Forms.MessageBox.Show("集合类" + typeof(T2).FullName + ",主键为空");
                }
                T1 t1 = (T1)value;
                if (dic.TryGetValue(t1, out outlist))
                {
                    outlist.Add(t2);
                }
                else
                {
                    outlist = new ObservableCollection <T2>();
                    outlist.Add(t2);
                    dic.Add(t1, outlist);
                }
            }
            return(dic);
        }