动态添加实体的值
public class DynamicBuilder<T> { private PropertyInfo[] properties; private DynamicBuilder() { } public T Build(IDataRecord reader) { T result = (T)Activator.CreateInstance(typeof(T)); for (int i = 0; i < reader.FieldCount; i++) { if (properties[i] != null && !reader.IsDBNull(i)) { if (properties[i].PropertyType.IsEnum) { properties[i].SetValue(result, Enum.ToObject(properties[i].PropertyType, reader[i]), null); } else { properties[i].SetValue(result, Convert.ChangeType(reader[i], properties[i].PropertyType), null); } } } return result; } public static DynamicBuilder<T> CreateBuilder(IDataRecord reader) { DynamicBuilder<T> result = new DynamicBuilder<T>(); result.properties = new PropertyInfo[reader.FieldCount]; for (int i = 0; i < reader.FieldCount; i++) { result.properties[i] = typeof(T).GetProperty(reader.GetName(i), BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); } return result; } }