public object GetObject(System.Data.DataSet ds,Type type)
{
if(ds==null)return null;
if(ds.Tables.Count<=0)return null;
if(ds.Tables[0].Rows.Count<=0)return null;
LHY.XMLConfiglib.Entity entity=LHY.XMLConfiglib.ConfigXML.AllEntity[type.FullName];
object obj=type.Assembly.CreateInstance(type.FullName,true);
foreach(LHY.XMLConfiglib.Attr at in entity.AttrCollection)
{
System.Reflection.PropertyInfo pinfo=obj.GetType().GetProperty(at.Name);
if(pinfo.CanWrite)
pinfo.SetValue(obj,Convert.ChangeType(ds.Tables[0].Rows[0][at.DBName],pinfo.PropertyType),null);
}
return obj;
}
其中主要的地方是Convert.ChangeType的应用,如果没有这个方法,那么将会出错。
本文介绍了一个通过C#中的Convert.ChangeType方法将DataSet中的数据转换为特定对象类型的实用方法。此方法确保了数据类型之间的正确转换,避免了运行时错误。
263

被折叠的 条评论
为什么被折叠?



