추가패치

This commit is contained in:
2025-03-05 10:00:08 +09:00
parent 8b39c28efb
commit fb46e2b17a
12 changed files with 356 additions and 139 deletions

View File

@@ -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>();