public static PropertyDescriptor Mapping(string name) { PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(mdbConcern)); switch (name) { case "Concern_Id": return(props.GetByName("ConcernId")); case "Concern": return(props.GetByName("Concern")); default: return(null); } }
protected override void Inject(IDataReader source, object target, PropertyDescriptorCollection targetProps) { var columns = source.GetSchemaTable().Columns; for (var i = 0; i < columns.Count; i++) { var c = columns[i]; var targetPropName = c.ColumnName; //default is the same as columnName if (c.ColumnName == "Foo") { targetPropName = "TheTargetPropForFoo"; } if (c.ColumnName == "Bar") { targetPropName = "TheTargetPropForBar"; } //you could also create a dictionary and use it here var targetProp = targetProps.GetByName(targetPropName); //go to next column if there is no such property in the target object if (targetProp == null) { continue; } targetProp.SetValue(target, columns[c.ColumnName]); } }
/// <summary> /// Search for a PropertyDescriptor within the collection that is of a specific type T /// </summary> /// <returns>search result or null if nothing was found</returns> public static PropertyDescriptor GetByNameType <T>(this PropertyDescriptorCollection collection, string name) { var p = collection.GetByName(name); if (p != null && p.PropertyType == typeof(T)) { return(p); } return(null); }
protected override void Inject(IDataReader source, object target, PropertyDescriptorCollection targetProps) { for (var i = 0; i < source.FieldCount; i++) { var activeTarget = targetProps.GetByName(source.GetName(i), true); if (activeTarget == null) { continue; } var value = source.GetValue(i); if (value == DBNull.Value) { continue; } activeTarget.SetValue(target, value); } }