示例#1
0
        /// <summary>
        /// 单一类型集合,如果Object类型不一致,请手动使用GetSheetData(object o)
        /// </summary>
        /// <param name="objects"></param>
        /// <returns></returns>
        public static List <Dictionary <string, object> > GetSheetData(List <object> objects)
        {
            List <Dictionary <string, object> > dic = new List <Dictionary <string, object> >();

            if (!objects.Any())
            {
                return(dic);
            }
            Type type       = objects[0].GetType();
            var  properties = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
            Dictionary <string, PropertyInfo> pros = new Dictionary <string, PropertyInfo>();

            foreach (var property in properties)
            {
                try
                {
                    if ((property.PropertyType.IsClass && property.PropertyType != typeof(string)) || property.PropertyType.IsAbstract)
                    {
                        continue;
                    }
                    TSheetFileAttribute[] attrs =
                        property.GetCustomAttributes(typeof(TSheetFileAttribute), true) as TSheetFileAttribute[];
                    if (attrs.Any())
                    {
                        pros[attrs[0].FileName] = property;
                    }
                }
                catch (Exception)
                {
                }
            }
            for (int i = 0; i < objects.Count; i++)
            {
                object      tempObject = objects[i];
                ITSheetData iTSheet    = tempObject as ITSheetData;
                if (iTSheet != null)
                {
                    dic.AddRange(iTSheet.CreateSheetData());
                    continue;
                }
                Dictionary <string, object> tempDicObject = new Dictionary <string, object>();
                foreach (var propertyInfo in pros)
                {
                    tempDicObject.Add(propertyInfo.Key, propertyInfo.Value.GetValue(tempObject));
                }
                dic.Add(tempDicObject);
            }
            return(dic);
        }
示例#2
0
        public static List <Dictionary <string, object> > GetSheetData(object o)
        {
            List <Dictionary <string, object> > result = new List <Dictionary <string, object> >();
            ITSheetData getData = o as ITSheetData;

            if (getData != null)
            {
                result.AddRange(getData.CreateSheetData());
                return(result);
            }
            Dictionary <string, object> dic = new Dictionary <string, object>();
            Type type       = o.GetType();
            var  properties = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

            foreach (var property in properties)
            {
                try
                {
                    if ((property.PropertyType.IsClass && property.PropertyType != typeof(string)) || property.PropertyType.IsAbstract)
                    {
                        continue;
                    }
                    TSheetFileAttribute[] attrs =
                        property.GetCustomAttributes(typeof(TSheetFileAttribute), true) as TSheetFileAttribute[];
                    if (attrs.Any())
                    {
                        dic[attrs[0].FileName] = property.GetValue(o);
                    }
                }
                catch (Exception)
                {
                }
            }
            result.Add(dic);
            return(result);
        }