public void Load(DataSet dataSet, object root) { Type type = root.GetType(); string tableName = type.Name; object[] classConfigAttributes = type.GetCustomAttributes(typeof(ClassConfigAttribute), true); if (classConfigAttributes.Length == 1 && classConfigAttributes[0] is ClassConfigAttribute) { ClassConfigAttribute classConfigAttribute = (ClassConfigAttribute)classConfigAttributes[0]; if (!string.IsNullOrEmpty(classConfigAttribute.TableName)) { tableName = classConfigAttribute.TableName; } } if (dataSet.Tables.Contains(tableName)) { DataTable dataTable = dataSet.Tables[tableName]; if (dataTable.Rows.Count == 1) { Load(dataTable.Rows[0], root); } } }
public DataSet ConvertToDataSet(object o) { DataSet dataSet = new DataSet(); Type type = o.GetType(); string tableName = type.Name; object[] classConfigAttributes = type.GetCustomAttributes(typeof(ClassConfigAttribute), true); if (classConfigAttributes.Length == 1 && classConfigAttributes[0] is ClassConfigAttribute) { ClassConfigAttribute classConfigAttribute = (ClassConfigAttribute)classConfigAttributes[0]; if (!string.IsNullOrEmpty(classConfigAttribute.TableName)) { tableName = classConfigAttribute.TableName; } } AddRow(o, dataSet, tableName); return(dataSet); }
private void Load(DataRow dataRow, object o) { Type type = o.GetType(); PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); foreach (PropertyInfo propertyInfo in properties) { string relationName; object[] propertyAttributes = propertyInfo.GetCustomAttributes(typeof(PropertyAttribute), true); if (propertyAttributes.Length == 1) { PropertyAttribute propertyAttribute = (PropertyAttribute)propertyAttributes[0]; switch (propertyAttribute.PropertyType) { case PropertyType.Column: if (dataRow.Table.Columns.Contains(propertyInfo.Name)) { DataColumn dataColumn = dataRow.Table.Columns[propertyInfo.Name]; object value = dataRow[dataColumn]; if (propertyInfo.GetSetMethod() != null) { if (value is DBNull || (dataColumn.DataType is object && value.ToString().ToLower() == "null")) { value = null; } else { if (propertyInfo.PropertyType.BaseType != null && propertyInfo.PropertyType.BaseType.FullName == "System.Enum") { value = Enum.Parse(propertyInfo.PropertyType, value.ToString()); } } propertyInfo.SetValue(o, value, null); } } break; case PropertyType.Parent: if (dataRow.Table.Columns.Contains(propertyInfo.Name)) { DataColumn dataColumn = dataRow.Table.Columns[propertyInfo.Name]; ParentPropertyAttribute parentPropertyAttribute = (ParentPropertyAttribute)propertyAttribute; relationName = parentPropertyAttribute.RelationName ?? propertyInfo.Name; DataRow parentRow = dataRow.GetParentRow(relationName); object parent = propertyInfo.GetValue(o, null); if (parentRow == null) { if (parent != null) { if (propertyInfo.GetSetMethod() != null) { propertyInfo.SetValue(o, null, null); } } } else { if (parent == null) { Type propertyType = propertyInfo.PropertyType; object[] classAttributes = propertyType.GetCustomAttributes(typeof(ClassConfigAttribute), true); if (classAttributes.Length == 1 && classAttributes[0] is ClassConfigAttribute) { ClassConfigAttribute classAttribute = (ClassConfigAttribute)classAttributes[0]; if (!string.IsNullOrEmpty(classAttribute.DerivedTypesColumnName)) { PropertyInfo derivedTypesProperty = propertyType.GetProperty(classAttribute.DerivedTypesColumnName); if (derivedTypesProperty != null) { object[] derivedTypesPropertyAttributes = derivedTypesProperty.GetCustomAttributes(typeof(DerivedTypePropertyAttribute), true); if (derivedTypesPropertyAttributes.Length > 0) { if (dataRow[classAttribute.DerivedTypesColumnName] != null) { string typeNickName = dataRow[classAttribute.DerivedTypesColumnName].ToString(); foreach (DerivedTypePropertyAttribute derivedTypePropertyAttribute in derivedTypesPropertyAttributes) { if (derivedTypePropertyAttribute.Name == typeNickName) { parent = System.Activator.CreateInstance(derivedTypePropertyAttribute.Type); propertyInfo.SetValue(o, parent, null); break; } } } } } } } if (parent == null) { if (!parentPropertyAttribute.NonRecursive) { parent = System.Activator.CreateInstance(propertyType); propertyInfo.SetValue(o, parent, null); } } } if (!parentPropertyAttribute.NonRecursive) { Load(parentRow, parent); } } } break; case PropertyType.Children: ChildrenPropertyAttribute childrenPropertyAttribute = (ChildrenPropertyAttribute)propertyAttribute; relationName = childrenPropertyAttribute.RelationName ?? propertyInfo.Name; DataRow[] childrenRows = dataRow.GetChildRows(relationName); object children = propertyInfo.GetValue(o, null); object child = null; if (children != null) { bool isDictionary = children is IDictionary; IDictionary dictionary = null; DataColumn dictionaryKeyColumn = null; if (isDictionary) { dictionary = (IDictionary)children; string dictionaryKeyColumnName = childrenPropertyAttribute.DictionaryKeyColumnName; DataRelation dataRelation = dataRow.Table.DataSet.Relations[relationName]; DataTable childrenTable = dataRelation.ChildTable; if (childrenTable.Columns.Contains(dictionaryKeyColumnName)) { dictionaryKeyColumn = childrenTable.Columns[dictionaryKeyColumnName]; } } foreach (DataRow childRow in childrenRows) { if (isDictionary && dictionaryKeyColumn != null) { object key = childRow[dictionaryKeyColumn]; if (dictionary.Contains(key)) { child = dictionary[key]; } } else if (children is IList) { child = Activator.CreateInstance(childrenPropertyAttribute.Type); ((IList)children).Add(child); } else { throw new NotSupportedException(); } if (child != null) { Load(childRow, child); } } } break; default: throw new NotSupportedException(); } } } }
public void Load(DataRow dataRow, object o, Dictionary <DataRow, object> loadedObjects) { Type type = o.GetType(); PropertyInfo[] properties = GetProperties(type); foreach (PropertyInfo propertyInfo in properties) { string relationName; //object[] propertyAttributes = propertyInfo.GetCustomAttributes(typeof(PropertyAttribute), true); PropertyAttribute propertyAttribute = GetPropertyAttribute(propertyInfo); if (propertyAttribute != null) { //PropertyAttribute propertyAttribute = (PropertyAttribute)propertyAttributes[0]; switch (propertyAttribute.PropertyType) { case PropertyType.Column: if (dataRow.Table.Columns.Contains(propertyInfo.Name)) { DataColumn dataColumn = dataRow.Table.Columns[propertyInfo.Name]; object value = dataRow[dataColumn]; if (propertyInfo.GetSetMethod() != null) { if (value is DBNull || (dataColumn.DataType is object && value.ToString().ToLower() == "null")) { value = null; if (propertyAttribute.Default != null) { value = propertyAttribute.Default; } } else { if (propertyInfo.PropertyType.BaseType != null && propertyInfo.PropertyType.BaseType.FullName == "System.Enum") { if (Enum.IsDefined(propertyInfo.PropertyType, value.ToString())) { value = Enum.Parse(propertyInfo.PropertyType, value.ToString()); } else { value = null; } } } try { propertyInfo.SetValue(o, value, null); } catch { } } } break; case PropertyType.Parent: if (dataRow.Table.Columns.Contains(propertyInfo.Name)) { DataColumn dataColumn = dataRow.Table.Columns[propertyInfo.Name]; ParentPropertyAttribute parentPropertyAttribute = (ParentPropertyAttribute)propertyAttribute; relationName = parentPropertyAttribute.RelationName ?? propertyInfo.Name; DataRow parentRow = dataRow.GetParentRow(relationName); object parent = propertyInfo.GetValue(o, null); if (parentRow == null) { if (parent != null) { if (propertyInfo.GetSetMethod() != null) { propertyInfo.SetValue(o, null, null); } } } else { if (parent == null) { Type propertyType = propertyInfo.PropertyType; object[] classAttributes = propertyType.GetCustomAttributes(typeof(ClassConfigAttribute), true); if (loadedObjects.ContainsKey(parentRow)) { parent = loadedObjects[parentRow]; propertyInfo.SetValue(o, parent, null); } else { if (classAttributes.Length == 1 && classAttributes[0] is ClassConfigAttribute) { ClassConfigAttribute classAttribute = (ClassConfigAttribute)classAttributes[0]; if (!string.IsNullOrEmpty(classAttribute.DerivedTypesColumnName)) { PropertyInfo derivedTypesProperty = propertyType.GetProperty(classAttribute.DerivedTypesColumnName); if (derivedTypesProperty != null) { object[] derivedTypesPropertyAttributes = derivedTypesProperty.GetCustomAttributes(typeof(DerivedTypePropertyAttribute), true); if (derivedTypesPropertyAttributes.Length > 0) { if (dataRow[classAttribute.DerivedTypesColumnName] != null) { string typeNickName = dataRow[classAttribute.DerivedTypesColumnName].ToString(); foreach (DerivedTypePropertyAttribute derivedTypePropertyAttribute in derivedTypesPropertyAttributes) { if (derivedTypePropertyAttribute.Name == typeNickName) { parent = System.Activator.CreateInstance(derivedTypePropertyAttribute.Type); loadedObjects.Add(parentRow, parent); propertyInfo.SetValue(o, parent, null); break; } } } } } } } } if (parent == null) { parent = System.Activator.CreateInstance(propertyType); loadedObjects.Add(parentRow, parent); propertyInfo.SetValue(o, parent, null); } } Load(parentRow, parent, loadedObjects); } } break; case PropertyType.Children: ChildrenPropertyAttribute childrenPropertyAttribute = (ChildrenPropertyAttribute)propertyAttribute; relationName = childrenPropertyAttribute.RelationName ?? propertyInfo.Name; DataRow[] childrenRows = dataRow.GetChildRows(relationName); object children = propertyInfo.GetValue(o, null); object child = null; if (children != null) { bool isDictionary = children is IDictionary; IDictionary dictionary = null; DataColumn dictionaryKeyColumn = null; if (isDictionary) { dictionary = (IDictionary)children; string dictionaryKeyColumnName = childrenPropertyAttribute.DictionaryKeyColumnName; Type childrenType = childrenPropertyAttribute.Type; DataRelation dataRelation = dataRow.Table.DataSet.Relations[relationName]; if (dataRelation == null) { dataRelation = CompleteDataRelationStructure(dataRow.Table.DataSet, dataRow.Table, childrenPropertyAttribute.TableName ?? childrenType.Name, childrenType, propertyInfo, childrenPropertyAttribute); } DataTable childrenTable = dataRelation.ChildTable; if (childrenTable.Columns.Contains(dictionaryKeyColumnName)) { dictionaryKeyColumn = childrenTable.Columns[dictionaryKeyColumnName]; } } if (children is IList) { ((IList)children).Clear(); } foreach (DataRow childRow in childrenRows) { if (isDictionary && dictionaryKeyColumn != null) { object key = childRow[dictionaryKeyColumn]; if (dictionary.Contains(key)) { child = dictionary[key]; } else { child = null; try { if (!childrenPropertyAttribute.Type.IsAbstract) { child = Activator.CreateInstance(childrenPropertyAttribute.Type); dictionary.Add(key, child); } } catch { } } } else if (children is IList) { if (!loadedObjects.ContainsKey(childRow)) { child = Activator.CreateInstance(childrenPropertyAttribute.Type); ((IList)children).Add(child); } } else { throw new NotSupportedException(); } if (child != null) { if (!loadedObjects.ContainsKey(childRow)) { loadedObjects.Add(childRow, child); Load(childRow, child, loadedObjects); } } } } break; default: throw new NotSupportedException(); } } } //OnObjectLoaded(new ObjectLoadedEventArgs(dataRow, o, loadedObjects, this)); }