추가패치
This commit is contained in:
@@ -397,6 +397,46 @@ namespace JWH
|
||||
catch { throw; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a object to a list with generic objects
|
||||
/// dataTable.ToClass<Employee>();
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Generic object</typeparam>
|
||||
/// <param name="src">DataTable</param>
|
||||
/// <returns>List with generic objects</returns>
|
||||
public static T ToClass<T>(this object src) where T : class, new()
|
||||
{
|
||||
try
|
||||
{
|
||||
T des = new T();
|
||||
Type desType = des.GetType();
|
||||
Type srcType = src.GetType();
|
||||
foreach (PropertyInfo srcProperty in srcType.GetProperties())
|
||||
{
|
||||
try
|
||||
{
|
||||
PropertyInfo desProperty = desType.GetProperty(srcProperty.Name);
|
||||
if (desProperty == null) continue;
|
||||
|
||||
var srcValue = srcProperty.GetValue(src);
|
||||
if (srcProperty.PropertyType != desProperty.PropertyType)
|
||||
{
|
||||
srcValue = Convert.ChangeType(srcValue, desProperty.PropertyType);
|
||||
}
|
||||
|
||||
desProperty.SetValue(des, srcValue);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
XLogger.Instance.Warn(ex);
|
||||
}
|
||||
}
|
||||
|
||||
return des;
|
||||
}
|
||||
catch { throw; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a object to a list with generic objects
|
||||
/// dataTable.ToClass<Employee>();
|
||||
|
||||
Reference in New Issue
Block a user