更新时间:2023-11-23 21:54:52
为了利用反射,谷歌上没有可以这样做的例子。
Its better to make use of reflection there are no of example avaible on google to do this this.
查看下面的例子
namespace MyNamespace.Data
{
class Converter
{
public static void Fill(object LogicObject, DataRow Row)
{
Dictionary<string, PropertyInfo> props = new Dictionary<string,PropertyInfo>();
foreach (PropertyInfo p in LogicObject.GetType().GetProperties())
props.Add(p.Name, p);
foreach (DataColumn col in Row.Table.Columns)
{
string name = col.ColumnName;
if (Row[name] != DBNull.Value && props.ContainsKey(name))
{
object item = Row[name];
PropertyInfo p = props[name];
if (p.PropertyType != col.DataType)
item = Convert.ChangeType(item, p.PropertyType);
p.SetValue(LogicObject, item, null);
}
}
}
}
}
查看完整的博文: http://kasey-jo.blogspot.com/2009/04/using-reflection-to-fill-business-layer.html