/// <summary>Получить список (значение; наименование)</summary> /// <param name="enumType">Тип перечисления</param> /// <returns>Список (значение; наименование)</returns> public static Dictionary <int, string> GetList(Type enumType) { int index = 0; Dictionary <int, string> list = new Dictionary <int, string>(); while (true) { string numberStr = index.ToString(); object valueDescription = Enum.Parse(enumType, index.ToString(), true); string valueStr = valueDescription.ToString(); if (numberStr == valueStr) { break; } MemberInfo inf = enumType.GetMembers()[10 + index]; dbFieldAtt attribute = Attribute.GetCustomAttribute(inf, typeof(dbFieldAtt)) as dbFieldAtt; if (attribute != null) { list.Add(index++, attribute.Description); } } return(list); }
/// <summary>Копировать объект</summary> /// <param name="source">Объект-исходник</param> /// <returns>Скопированный объект</returns> public static dbObject Copy(dbObject source) { if (source != null) { Type type = source.GetType(); dbObject copy = (dbObject)Activator.CreateInstance(type); PropertyInfo[] propertyInfos = type.GetProperties(); foreach (PropertyInfo property in propertyInfos) { dbFieldAtt attributes = Attribute.GetCustomAttribute(property, typeof(dbFieldAtt)) as dbFieldAtt; if (attributes != null) { object value = property.GetValue(source, null); property.SetValue(copy, value, null); } } ISynced synced = copy as ISynced; if (synced != null) { synced.IsSynced = false; } IBarcodeOwner barcode = copy as IBarcodeOwner; if (barcode != null) { barcode.BarCode = string.Empty; } copy.SyncRef = string.Empty; copy.Id = 0; return(copy); } return(null); }
/// <summary>Получить наименование</summary> /// <param name="enumType">Тип перечисления</param> /// <param name="value">Значение</param> /// <returns>Наименование</returns> public static string GetDescription(Type enumType, int value) { FieldInfo[] fields = enumType.GetFields(); value++; if (fields.Length > value) { Attribute[] attributes = Attribute.GetCustomAttributes(fields[value]); foreach (Attribute attribute in attributes) { dbFieldAtt enumAttributes = attribute as dbFieldAtt; if (enumAttributes != null) { return(enumAttributes.Description); } } } return("Помилка: Тип не знайдно!"); }
/// <summary>Прочитать данные объекта с БД</summary> /// <param name="type">Тип объекта</param> /// <param name="id">Значение идентификатора</param> /// <param name="idColumn">Название колонки идентификатора</param> /// <returns>Объект</returns> public virtual object Read(Type type, object id, string idColumn) { PropertyInfo[] properties = type.GetProperties(); StringBuilder line = new StringBuilder(); foreach (PropertyInfo field in properties) { dbFieldAtt attributes = Attribute.GetCustomAttribute(field, typeof(dbFieldAtt)) as dbFieldAtt; if (attributes != null) { line.AppendFormat("[{0}],", field.Name); } } string command = string.Format("SELECT {0} FROM {1} WHERE {2}=@Id", line.ToString(0, line.Length - 1), type.Name, idColumn); using (SqlCeCommand query = dbWorker.NewQuery(command)) { query.AddParameter("Id", id); SqlCeDataReader reader = query.ExecuteReader(); while (reader.Read()) { foreach (PropertyInfo property in properties) { dbFieldAtt attributes = Attribute.GetCustomAttribute(property, typeof(dbFieldAtt)) as dbFieldAtt; if (attributes != null) { object value = reader[property.Name]; if (property.PropertyType == typeof(int)) { value = Convert.ToInt32(value); } if (property.PropertyType == typeof(long)) { value = Convert.ToInt64(value); } else if (property.PropertyType == typeof(double)) { value = Convert.ToDouble(value); } else if (property.PropertyType == typeof(string)) { value = value.ToString().TrimEnd(); } property.SetValue(this, value, null); } } } } //Если Id=0, значит такой обьект не найден -> он новый IsNew = Id == 0; return(this); }
/// <summary>Получить детальное(инф. по вложенным элементам, уровень вложености = 1) представление по элементу</summary> /// <param name="type">Тип объекта визуализирования</param> /// <param name="listOfDetail">Словарь єлементов с детальной информацией</param> /// <param name="accessory">Объект визуализирования</param> /// <returns>Список ...</returns> public static List <LabelForConstructor> GetDetailVisualPresenter(Type type, out Dictionary <string, KeyValuePair <Type, object> > listOfDetail, dbObject accessory) { listOfDetail = new Dictionary <string, KeyValuePair <Type, object> >(); List <LabelForConstructor> list = new List <LabelForConstructor>(); PropertyInfo[] fields = type.GetProperties(); foreach (PropertyInfo field in fields) { Attribute[] attributes = Attribute.GetCustomAttributes(field); foreach (Attribute a in attributes) { dbFieldAtt attribute = a as dbFieldAtt; if (attribute != null) { if (attribute.NeedDetailInfo) { object value = field.GetValue(accessory, null); listOfDetail.Add(attribute.Description, new KeyValuePair <Type, object>(attribute.dbObjectType, value)); } else if (attribute.ShowInEditForm) { object value = field.GetValue(accessory, null); if (attribute.dbObjectType == null) { if (field.PropertyType == typeof(DateTime)) { DateTime dateTimeValue = (DateTime)value; value = dateTimeValue == SqlDateTime.MinValue.Value ? string.Empty : string.Format("{0:dd.MM.yyyy}", dateTimeValue); } else if (field.PropertyType.IsEnum) { value = EnumWorker.GetDescription(field.PropertyType, Convert.ToInt32(value)); } else if (field.PropertyType == typeof(bool)) { value = (bool)value ? "+" : "-"; } else if (value.Equals(0) || value.Equals(0L) || value.Equals(0D)) { value = string.Empty; } } else { dbObject detailObject = (dbObject)Activator.CreateInstance(attribute.dbObjectType); detailObject = (dbObject)detailObject.Read(attribute.dbObjectType, value, IDENTIFIER_NAME); Dictionary <string, KeyValuePair <Type, object> > subListOfDetail; List <LabelForConstructor> subList = GetSingleVisualPresenter( attribute.dbObjectType, out subListOfDetail, detailObject, true); list.AddRange(subList); value = ReadDescription(attribute.dbObjectType, value); } string data = String.Format("{0}: {1}", attribute.Description, value); list.Add(new LabelForConstructor(field.Name, true, data, ControlsStyle.LabelSmall, false)); break; } } } } if (type == typeof(Lamps) || type == typeof(ElectronicUnits)) { listOfDetail.Add("Корпус", new KeyValuePair <Type, object>(typeof(Cases), accessory.GetPropery("Case"))); } return(list); }
/// <summary>Получить визуальное представление только по элементу(без вложенной инф.)</summary> /// <param name="type">Тип объекта визуализирования</param> /// <param name="listOfDetail">Словарь єлементов с детальной информацией</param> /// <param name="accessory">Объект визуализирования</param> /// <param name="isDetailMode">Режим детального списка?</param> /// <returns>Список ...</returns> public static List <LabelForConstructor> GetSingleVisualPresenter(Type type, out Dictionary <string, KeyValuePair <Type, object> > listOfDetail, dbObject accessory, bool isDetailMode) { listOfDetail = new Dictionary <string, KeyValuePair <Type, object> >(); List <LabelForConstructor> list = new List <LabelForConstructor>(); PropertyInfo[] fields = type.GetProperties(); foreach (PropertyInfo field in fields) { Attribute[] attributes = Attribute.GetCustomAttributes(field); foreach (Attribute a in attributes) { dbFieldAtt attribute = a as dbFieldAtt; if (attribute != null) { if (attribute.NeedDetailInfo) { object value = field.GetValue(accessory, null); listOfDetail.Add(attribute.Description, new KeyValuePair <Type, object>(attribute.dbObjectType, value)); } else if ((isDetailMode && attribute.ShowInEditForm) || (!isDetailMode && !attribute.NotShowInForm)) { object value = field.GetValue(accessory, null); if (attribute.dbObjectType == null) { if (field.PropertyType == typeof(DateTime)) { DateTime dateTimeValue = (DateTime)value; value = dateTimeValue == SqlDateTime.MinValue.Value ? string.Empty : String.Format("{0:dd.MM.yyyy}", dateTimeValue); } else if (field.PropertyType.IsEnum) { value = EnumWorker.GetDescription(field.PropertyType, Convert.ToInt32(value)); } else if (field.PropertyType == typeof(bool)) { value = (bool)value ? "+" : "-"; } else if (value.Equals(0) || value.Equals(0L) || value.Equals(0D)) { value = string.Empty; } } else { value = ReadDescription(attribute.dbObjectType, value); } string data = String.Format("{0}: {1}", attribute.Description, value); list.Add(new LabelForConstructor(data, ControlsStyle.LabelSmall, false)); break; } } } } return(list); }
/// <summary>Получить визуальное представление (с информацией о элементах на которые возможны переходы)</summary> /// <param name="typeOfAccessories">Тип комплектующего</param> /// <param name="accessory">Объект</param> /// <param name="topic">Заголовок</param> /// <param name="listOfDetail">Словарь єлементов с детальной информацией</param> /// <returns>Список ...</returns> public static List <LabelForConstructor> GetVisualPresenter(TypeOfAccessories typeOfAccessories, Accessory accessory, out string topic, out Dictionary <string, KeyValuePair <Type, object> > listOfDetail) { topic = Cases.GetDescriptionOfAccessory(typeOfAccessories); listOfDetail = new Dictionary <string, KeyValuePair <Type, object> >(); List <LabelForConstructor> list = new List <LabelForConstructor>(); bool notCase = (typeOfAccessories == TypeOfAccessories.ElectronicUnit || typeOfAccessories == TypeOfAccessories.Lamp); if (accessory != null) { Type type = accessory.GetType(); PropertyInfo[] fields = type.GetProperties(); if (notCase) { listOfDetail.Add("Корпус", new KeyValuePair <Type, object>(typeof(Cases), CatalogHelper.FindCaseId(accessory.Id, typeOfAccessories))); } foreach (PropertyInfo field in fields) { if (notCase && field.Name == "Case") { continue; } Attribute[] attributes = Attribute.GetCustomAttributes(field); foreach (Attribute a in attributes) { dbFieldAtt attribute = a as dbFieldAtt; if (attribute != null) { if (attribute.NeedDetailInfo) { object value = field.GetValue(accessory, null); listOfDetail.Add(attribute.Description, new KeyValuePair <Type, object>(attribute.dbObjectType, value)); } else if (!attribute.NotShowInForm || attribute.ShowEmbadedInfo) { object value = field.GetValue(accessory, null); if (attribute.dbObjectType == null) { if (field.PropertyType == typeof(DateTime)) { DateTime dateValue = (DateTime)value; value = dateValue != SqlDateTime.MinValue.Value ? String.Format("{0:dd.MM.yyyy}", dateValue) : string.Empty; } else if (field.PropertyType.IsEnum) { value = EnumWorker.GetDescription(field.PropertyType, Convert.ToInt32(value)); } else if (field.PropertyType == typeof(bool)) { value = (bool)value ? "+" : "-"; } } else { if (attribute.ShowEmbadedInfo) { dbObject detailObject = (dbObject)Activator.CreateInstance(attribute.dbObjectType); detailObject = (dbObject)detailObject.Read(attribute.dbObjectType, value, IDENTIFIER_NAME); Dictionary <string, KeyValuePair <Type, object> > subListOfDetail; List <LabelForConstructor> subList = GetSingleVisualPresenter( attribute.dbObjectType, out subListOfDetail, detailObject, false); list.AddRange(subList); } if (!attribute.NotShowInForm) { value = ReadDescription(attribute.dbObjectType, value); } } string data = String.Format("{0}: {1}", attribute.Description, value); list.Add(new LabelForConstructor(data, ControlsStyle.LabelSmall, false)); break; } } } } } return(list); }