private void CompleteDataTableStructure(object o, Type type, DataTable dataTable) { PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); foreach (PropertyInfo propertyInfo in properties) { object[] propertyAttributes = propertyInfo.GetCustomAttributes(typeof(PropertyAttribute), true); if (propertyAttributes.Length == 1 && propertyAttributes[0] is PropertyAttribute) { PropertyAttribute propertyAttribute = (PropertyAttribute)propertyAttributes[0]; switch (propertyAttribute.PropertyType) { case PropertyType.Column: ColumnPropertyAttribute columnPropertyAttribute = (ColumnPropertyAttribute)propertyAttribute; CompleteDataColumn(dataTable, propertyInfo, propertyAttribute); break; case PropertyType.Parent: ParentPropertyAttribute parentPropertyAttribute = (ParentPropertyAttribute)propertyAttribute; object parent = null; Type parentPropertyType = null; if (o == null) { parentPropertyType = propertyInfo.PropertyType; } else { parent = propertyInfo.GetValue(o, null); if (parent == null) { parentPropertyType = propertyInfo.PropertyType; } else { parentPropertyType = parent.GetType(); } } PropertyInfo[] parentProperties = parentPropertyType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); string parentTableName = parentPropertyAttribute.TableName; DataTable parentDataTable = GetDataTable(dataTable.DataSet, parentTableName); if (!parentPropertyAttribute.NonRecursive) { CompleteDataTableStructure(parent, parentPropertyType, parentDataTable); } DataColumn parentDataColumn = CompleteParentDataColumn(dataTable, propertyInfo, propertyAttribute, parentDataTable.PrimaryKey[0].DataType); CreateRelation(dataTable, parentDataTable, parentDataColumn, parentPropertyAttribute); break; case PropertyType.Children: ChildrenPropertyAttribute childrenPropertyAttribute = (ChildrenPropertyAttribute)propertyAttribute; Type childrenPropertyType = null; if (o == null) { childrenPropertyType = childrenPropertyAttribute.Type; CompleteChildrenStructure(null, childrenPropertyType, dataTable, propertyInfo, childrenPropertyAttribute); } else { object children = propertyInfo.GetValue(o, null); if (children != null) { IEnumerable list = null; if (children is IList) { list = (IEnumerable)children; } else if (children is IDictionary) { IDictionary dictionary = (IDictionary)children; list = (IEnumerable)dictionary.Values; } foreach (object child in list) { if (child != null) { childrenPropertyType = child.GetType(); //PropertyInfo[] childrenProperties = childrenPropertyType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); //string childrenTableName = childrenPropertyAttribute.TableName; //DataTable childrenDataTable = GetDataTable(dataTable.DataSet, childrenTableName); //CompleteDataTableStructure(child, childrenPropertyType, childrenDataTable); //DataColumn childrenDataColumn = CompleteParentDataColumn(childrenDataTable, propertyInfo, propertyAttribute, dataTable.PrimaryKey[0].DataType); //CreateRelation(childrenDataTable, dataTable, childrenDataColumn, childrenPropertyAttribute); CompleteChildrenStructure(child, childrenPropertyType, dataTable, propertyInfo, childrenPropertyAttribute); } } } } break; default: throw new NotSupportedException(); } } } }
private void CompleteChildrenStructure(object child, Type childrenPropertyType, DataTable dataTable, PropertyInfo propertyInfo, ChildrenPropertyAttribute childrenPropertyAttribute) { PropertyInfo[] childrenProperties = childrenPropertyType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); string childrenTableName = childrenPropertyAttribute.TableName; DataTable childrenDataTable = GetDataTable(dataTable.DataSet, childrenTableName); CompleteDataTableStructure(child, childrenPropertyType, childrenDataTable); DataColumn childrenDataColumn = CompleteParentDataColumn(childrenDataTable, propertyInfo, childrenPropertyAttribute, dataTable.PrimaryKey[0].DataType); CreateRelation(childrenDataTable, dataTable, childrenDataColumn, childrenPropertyAttribute); }
private void FillDataRow(object o, DataRow dataRow, PropertyInfo[] properties, DataTable dataTable) { foreach (PropertyInfo propertyInfo in properties) { object[] propertyAttributes = propertyInfo.GetCustomAttributes(typeof(PropertyAttribute), true); if (propertyAttributes.Length == 1 && propertyAttributes[0] is PropertyAttribute) { PropertyAttribute propertyAttribute = (PropertyAttribute)propertyAttributes[0]; switch (propertyAttribute.PropertyType) { case PropertyType.Column: SetColumnPropertyValue(o, propertyInfo, propertyAttribute, dataTable, dataRow); break; case PropertyType.Parent: ParentPropertyAttribute parentPropertyAttribute = (ParentPropertyAttribute)propertyAttribute; string parentTableName = parentPropertyAttribute.TableName; object parent = propertyInfo.GetValue(o, null); if (parent != null) { DataRow parentRow = AddRow(parent, dataTable.DataSet, parentTableName); dataRow.SetParentRow(parentRow); } break; case PropertyType.Children: ChildrenPropertyAttribute childrenPropertyAttribute = (ChildrenPropertyAttribute)propertyAttribute; string childrenTableName = childrenPropertyAttribute.TableName; object children = propertyInfo.GetValue(o, null); if (children != null) { IEnumerable list = null; if (children is IDictionary) { list = (IEnumerable)((IDictionary)children).Values; } else if (children is IList) { list = (IEnumerable)children; } if (list != null) { foreach (object child in list) { if (child != null) { DataRow childRow = AddRow(child, dataTable.DataSet, childrenTableName); childRow.SetParentRow(dataRow); } } } } break; default: throw new NotSupportedException(); } } } }
public void LoadMetaConfiguration(object o) { Type type = o.GetType(); PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); foreach (PropertyInfo propertyInfo in properties) { object[] propertyAttributes = propertyInfo.GetCustomAttributes(typeof(PropertyAttribute), true); if (propertyAttributes.Length == 1) { PropertyAttribute propertyAttribute = (PropertyAttribute)propertyAttributes[0]; object[] configValueAttributes = propertyInfo.GetCustomAttributes(typeof(ConfigValueAttribute), true); foreach (ConfigValueAttribute configValueAttribute in configValueAttributes) { if (type.Equals(configValueAttribute.Type)) { propertyInfo.SetValue(o, configValueAttribute.Value, null); } } switch (propertyAttribute.PropertyType) { case PropertyType.Column: break; case PropertyType.Parent: object parent = propertyInfo.GetValue(o, null); if (parent != null) { LoadMetaConfiguration(parent); } break; case PropertyType.Children: ChildrenPropertyAttribute childrenPropertyAttribute = (ChildrenPropertyAttribute)propertyAttribute; object children = propertyInfo.GetValue(o, null); if (children != null) { IEnumerable list = null; if (children is IDictionary) { list = (IEnumerable)((IDictionary)children).Values; } else if (children is IList) { list = (IEnumerable)children; } else { throw new NotSupportedException(); } if (list != null) { foreach (object child in list) { if (child != null) { LoadMetaConfiguration(child); } } } } break; default: throw new NotSupportedException(); } } } }
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(); } } } }
private void FillDataRow(object o, DataRow dataRow, PropertyInfo[] properties, DataTable dataTable, Dictionary <object, DataRow> loadedRows) { foreach (PropertyInfo propertyInfo in properties) { object[] propertyAttributes = propertyInfo.GetCustomAttributes(typeof(PropertyAttribute), true); if (propertyAttributes.Length == 1 && propertyAttributes[0] is PropertyAttribute) { PropertyAttribute propertyAttribute = (PropertyAttribute)propertyAttributes[0]; switch (propertyAttribute.PropertyType) { case PropertyType.Column: SetColumnPropertyValue(o, propertyInfo, propertyAttribute, dataTable, dataRow); break; case PropertyType.Parent: ParentPropertyAttribute parentPropertyAttribute = (ParentPropertyAttribute)propertyAttribute; string parentTableName = parentPropertyAttribute.TableName; object parent = propertyInfo.GetValue(o, null); if (parent != null) { DataRow parentRow; if (loadedRows.ContainsKey(parent)) { parentRow = loadedRows[parent]; } else { parentRow = AddRow(parent, dataTable.DataSet, parentTableName, loadedRows); loadedRows.Add(parent, parentRow); } dataRow.SetParentRow(parentRow); } break; case PropertyType.Children: ChildrenPropertyAttribute childrenPropertyAttribute = (ChildrenPropertyAttribute)propertyAttribute; string childrenTableName = childrenPropertyAttribute.TableName; object children = propertyInfo.GetValue(o, null); if (children != null) { IEnumerable list = null; if (children is IDictionary) { list = (IEnumerable)((IDictionary)children).Values; } else if (children is IList) { list = (IEnumerable)children; } if (list != null) { bool hasChildren = false; foreach (object child in list) { if (child != null) { hasChildren = true; DataRow childRow = AddRow(child, dataTable.DataSet, childrenTableName, loadedRows); childRow.SetParentRow(dataRow); } } if (!hasChildren) { CompleteDataRelationStructure(dataTable.DataSet, dataTable, childrenPropertyAttribute.TableName ?? childrenPropertyAttribute.Type.Name, childrenPropertyAttribute.Type, propertyInfo, childrenPropertyAttribute); } } } 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)); }