⽤反射,将DataRow⾏转为Object对象
///<summary>
///反射辅助类
///</summary>
public class ReflectionHelper
{
///<summary>
///获取类型
///</summary>
///<param name="typeAndAssName"></param>
///<returns></returns>
public static Type GetType(string typeAndAssName)
{
string[] strArray = typeAndAssName.Split(new char[] { ',' });
if (strArray.Length < 2)
{
return Type.GetType(typeAndAssName);
}
typeof arrayreturn GetType(strArray[0].Trim(), strArray[1].Trim());
}
///<summary>
///获取类型
/
//</summary>
///<param name="typeFullName"></param>
///<param name="assemblyName"></param>
///<returns></returns>
public static Type GetType(string typeFullName, string assemblyName)
{
if (assemblyName == null)
{
return Type.GetType(typeFullName);
}
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly assembly in assemblies)
{
if (assembly.FullName.Split(new char[] { ',' })[0].Trim() == assemblyName.Trim())
{
return assembly.GetType(typeFullName);
}
}
Assembly assembly2 = Assembly.Load(assemblyName);
if (assembly2 != null)
{
return assembly2.GetType(typeFullName);
}
return null;
}
}
///<summary>
///转换DataRow到实体对象
///</summary>
///<param name="objType"></param>
///<param name="row"></param>
///<returns></returns>
public static object ConvertRowToObject( Type objType , DataRow row )
{
if ( row == null )
{
return null;
}
DataTable table = row.Table;
object target = Activator.CreateInstance( objType );
foreach ( DataColumn column in table.Columns )
{
PropertyInfo property = objType.GetProperty( column.ColumnName , BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase );
if ( property == null )
{
throw new PropertyNotFoundException( column.ColumnName );
}
Type propertyType = property.PropertyType;
object obj3 = null;
bool flag = true;
try
{
obj3 = TypeHelper.ChangeType( propertyType , row[ column.ColumnName ] );
}
catch
{
flag = false;
}
if ( flag )
{
object[ ] args = new object[ ] { obj3 };
objType.InvokeMember( column.ColumnName , BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase , null , target , args );
}
}
return target;
}
///<summary>
///类型辅助类
///</summary>
public class TypeHelper
{
///<summary>
///调整对象类型
///</summary>
///<param name="targetType"></param>
///<param name="val"></param>
///<returns></returns>
public static object ChangeType(Type targetType, object val)
{
if (val == null)
{
return null;
}
if (targetType == val.GetType())
{
return val;
}
}
if (targetType == typeof(bool))
{
if (val.ToString() == "0")
{
return false;
}
if (val.ToString() == "1")
{
return true;
}
}
if (targetType.IsEnum)
{
int result = 0;
if (!int.TryParse(val.ToString(), out result))
{
return Enum.Parse(targetType, val.ToString());
}
return val;
}
if (targetType == typeof(Type))
{
return ReflectionHelper.GetType(val.ToString());
}
return Convert.ChangeType(val, targetType);
}
public static string GetClassSimpleName(Type t)
{
string[] strArray = t.ToString().Split(new char[] { '.' });
return strArray[strArray.Length - 1].ToString();
}
public static string GetDefaultValue(Type destType)
{
if (IsNumbericType(destType))
{
return"0";
}
if (destType == typeof(string))
{
return"\"\"";
}
if (destType == typeof(bool))
{
return"false";
}
if (destType == typeof(DateTime))
{
return"DateTime.Now";
}
if (destType == typeof(Guid))
{
return"System.Guid.NewGuid()";
}
if (destType == typeof(TimeSpan))
{
return"System.TimeSpan.Zero";
}
return"null";
}
public static Type GetTypeByRegularName(string regularName) {
return ReflectionHelper.GetType(regularName);
}
public static string GetTypeRegularName(Type destType)
{
string str = destType.Assembly.FullName.Split(new char[] { ',' })[0];
return string.Format("{0},{1}", destType.ToString(), str);
}
public static string GetTypeRegularNameOf(object obj)
{
return GetTypeRegularName(obj.GetType());
}
public static bool IsFixLength(Type destDataType)
{
return (IsNumbericType(destDataType) || ((destDataType == typeof(byte[])) || ((destDataType == typeof(DateTime)) || (destDataType == typeof(bool)))));
}
public static bool IsNumbericType(Type destDataType)
{
return ((((((destDataType == typeof(int)) || (destDataType == typeof(uint))) || ((destDataType == typeof(double)) || (destDataType ==
typeof(short)))) || (((destDataType == typeof(ushort)) || (destDataType == typeof(decimal))) || ((destDataType == typeof(long)) || (destDataType == typeof(ulong))))) || ((destDataType == typeof(float)) || (destDataType == typeof(byte)))) || (destDataType == typeof(sbyte)));
}
public static bool IsSimpleType(Type t)
{
return (IsNumbericType(t) || ((t == typeof(char)) || ((t == typeof(string)) || ((t == typeof(bool)) || ((t == typeof(DateTime)) || ((t == typeof(Type)) || t.IsEnum))))));
}
}
两个类,和⼀个反射⽅法。直接复制 使⽤
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论