/// <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 < )
{
return Type.GetType(typeAndAssName);
}
return GetType(strArray[].Trim(), strArray[].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[] { ',' })[].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() == "")
{
return false;
}
if (val.ToString() == "")
{
return true;
}
}
if (targetType.IsEnum)
{
int result = ;
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 - ].ToString();
} public static string GetDefaultValue(Type destType)
{
if (IsNumbericType(destType))
{
return "";
}
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[] { ',' })[];
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))))));
}
}

两个类,和一个反射方法。直接复制 使用

用反射,将DataRow行转为Object对象的更多相关文章

  1. 菜鸟类库诞生记二:通过反射转换DataRow为对象

    虽然大数据量的环境下,通过反射转换DataRow为对象性能会很低,但是在数据量适中的时候,这样能够减少很多的代码量,性能也确实不错. 所以在数据量不是很大的情况下,推荐使用. 如果数据量很大,可以使用 ...

  2. c# 将object尝试转为指定对象

    主方法: /// <summary> /// 将object尝试转为指定对象 /// </summary> /// <param name="data" ...

  3. java利用反射将pojo转为json对象

    最近做以太坊钱包项目需要与前台进行json交互,写一个工具类,经普通javaBean转为json对象 package util; import java.lang.reflect.Field; imp ...

  4. 使用java中的反射获得object对象的属性值

    知识点:使用java中的反射获得object对象的属性值 一:场景 这两天开发代码时,调用别人的后台接口,返回值为Object对象(json形式的),我想获得object中指定的属性值,没有对应的ge ...

  5. Java Object 对象创建的方式 [ 转载 ]

    Java Object 对象创建的方式 [ 转载 ] @author http://blog.csdn.net/mhmyqn/article/details/7943411 显式创建 有4种显式地创建 ...

  6. 让策划也能轻松修改数据的方法:运用Excel2Json2Object插件将xml表格转为Object导入脚本

    让策划也能轻松修改数据的方法:运用Excel2Json2Object插件将xml表格转为Object导入脚本 运用Excel2Json2Object插件将xml表格转为Object导入脚本 下载地址 ...

  7. DataTable转List,DataTable转为Model对象帮助类

    DataTable转List,DataTable转为Model对象帮助类 public class ModelConvertHelper<T> where T : new() { publ ...

  8. 原生JS:Object对象详细参考

    Object对象详细参考 本文参考MDN做的详细整理,方便大家参考MDN JavaScript原生提供一个Object对象(注意起首的O是大写),所有其他对象都继承自这个对象. 构造函数: Objec ...

  9. Object对象类

    Object对象类是所有类的祖先,他是默认自动继承的 Java为什么要做一个对象类呢?对象类的目的就是归一了类型,他就是把所有的类所有的对象归纳成为 Object类型.因为对象他认为对象应该拥有一些什 ...

随机推荐

  1. Identity-修改Error错误提示为中文

    第一步:重写IdentityErrorDescriber public class CustomIdentityErrorDescriber : IdentityErrorDescriber     ...

  2. QT 遍历目录查找指定文件(比较简单)

    QString FindFile(const QString &strFilePath, const QString &strNameFilters){ if (strFilePath ...

  3. CentOS 6.5(64bit)安装GCC4.8.2+Qt5.2.1(替换GCC的链接库)

    截至目前,Qt的最新版本为5.2.1,CentOS的版本为6.5,GCC的版本为4.8.2,经过一番尝试,终于将Qt开发环境安装到了CentOS(64 bit)中,整个过程中有几个需要注意的地方,在这 ...

  4. extjs 优化小建议

    1 原文信息 原文标题: Sencha Con 2013: Ext JS Performance tips 原文地址: [http://edspencer.net/2013/07/19/sencha- ...

  5. POJ--1300--Door Man【推断无向图欧拉通路】

    链接:http://poj.org/problem?id=1300 题意:有n个房间.每一个房间有若干个门和别的房间相连.管家从m房间開始走.要回到自己的住处(0),问是否有一条路能够走遍全部的门而且 ...

  6. 一次搞定cocos2d-x的 Android.mk 文件

    cocos2d-x 打 android 包时要修改 Android.mk 文件,但每次修改很麻烦,如果源文件很多的话就坑死了,也可以写一个脚本来自动生成这个文件,但每次还要重新生成一下,将其修改下面的 ...

  7. Android FragmentPagerAdapter和FragmentStatePagerAdapter的区别

    FragmentPagerAdapter官方解释: This version of the pager is best for use when there are a handful of typi ...

  8. PHP - 直接输出对象的版本问题

  9. 循环调用修正sic86

    create or replace procedure rebuild_sic86_wyl(pi_aac001 in number, po_fhz out varchar2, po_msg out v ...

  10. php利用iframe实现无刷新文件上传功能

    上传原理很简单就是利用表单的打开方式为iframe的name名,这样就可以在当前页面的iframe打来了,实现文件上传,再利用js返回上传结果. form target .在 action 属性中规定 ...