private object CallGetAttributeObject(string attributeName, object obj) { AttributeDictionaryPathable pathable = obj as AttributeDictionaryPathable; if (pathable != null) { return(pathable.GetAttributeValue(attributeName)); } AssumedTypes.IList assumedList = obj as AssumedTypes.IList; if (assumedList != null) { return(GetAttributeObject(attributeName, assumedList)); } System.Collections.IList list = obj as System.Collections.IList; if (list != null) { return(GetAttributeObject(attributeName, list)); } string openEhrV1AttributeName = GetOpenEhrV1AttributeName(attributeName, obj); System.ComponentModel.PropertyDescriptorCollection propertyDescriptorCollection = System.ComponentModel.TypeDescriptor.GetProperties(obj); System.ComponentModel.PropertyDescriptor property = propertyDescriptorCollection.Find(openEhrV1AttributeName, true); if (property == null) { property = propertyDescriptorCollection.Find(attributeName, false); } if (property == null) { return(null); } object attributeObj = property.GetValue(obj); return(attributeObj); }
public System.Data.DataTable ConvertToDataTable <T>(IList <T> data, string[] selection) { System.ComponentModel.PropertyDescriptorCollection properties = System.ComponentModel.TypeDescriptor.GetProperties(typeof(T)); System.Data.DataTable table = new System.Data.DataTable(); //order arrangement foreach (string selectedName in selection) { System.ComponentModel.PropertyDescriptor prop = properties.Find(selectedName, true); if (prop != null) { if ((!prop.PropertyType.IsGenericType && selection != null && selection.Contains(prop.Name.Trim())) || (selection != null && prop.PropertyType.IsGenericType && selection.Contains(prop.Name.Trim()) && (selection != null && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>)))) { table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType); } else if ((!prop.PropertyType.IsGenericType && selection == null) || (selection == null && prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>))) { table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType); } } } //foreach (System.ComponentModel.PropertyDescriptor prop in properties) // if ((!prop.PropertyType.IsGenericType && selection != null && selection.Contains(prop.Name.Trim())) || (selection != null && prop.PropertyType.IsGenericType && selection.Contains(prop.Name.Trim()) && (selection != null && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>)))) // table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType); // else if ((!prop.PropertyType.IsGenericType && selection == null) || (selection == null && prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))) // table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType); foreach (T item in data) { System.Data.DataRow row = table.NewRow(); foreach (System.ComponentModel.PropertyDescriptor prop in properties) { if ((!prop.PropertyType.IsGenericType && selection != null && selection.Contains(prop.Name.Trim())) || (selection != null && prop.PropertyType.IsGenericType && selection.Contains(prop.Name.Trim()) && (selection != null && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>)))) { row[prop.Name] = prop.GetValue(item) ?? DBNull.Value; } else if ((!prop.PropertyType.IsGenericType && selection == null) || (selection == null && prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>))) { row[prop.Name] = prop.GetValue(item) ?? DBNull.Value; } } table.Rows.Add(row); } return(table); }