这是自动进行1-1 Table.Column-Class.Property匹配的过程的一部分。我试着把每种可能的碱基类型
listed here
. 我已经越来越多地使用ORM,但是现在我回到了一个他们正在做的环境中
Id = row["Id"] as int
但我并没有用那种暴力的方式来进行强制转换,而是在属性上使用属性,这样我就可以编写
DataTable.ExtractAs<MyClass>
它试图创造和
IList<MyClass>
基于这些属性。
我的问题是:有没有更好的方法来处理从SqlDataType到覆盖所有整型的C#enum的转换?
现在我要做的是:
internal static bool TrySetValue<T>(T t, PropertyInfo property, DataRow row, string columnName)
{
Type propertyType = property.PropertyType;
// other logic and unique cases
if (propertyType.IsEnum
&& Enum.GetUnderlyingType(propertyType) == value.GetType())
{
property.SetValue(t, value, null);
return true;
}
}