/// <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. openstack、kvm CentOS升级内核

    openstack平台需要使用各种Linux发行版镜像,其制作方法主要有两种,要么是基于各大Linux发行版ISO光盘手动制作,要么是使用官方提供的制作好镜像进行修改 问题 FATAL: Module ...

  2. Hadoop平台安装前准备

    集群配置 准备工作 1.  Iptables #chkconfig iptables –list #chkconfig iptables –level 3456off #service iptable ...

  3. 高质量程序设计指南C/C++语言——C++/C编译预处理

    C++/C的编译预处理器对预编译伪指令进行处理后生成中间文件作为编译器的输入,因此所有的预编译伪指令都不会进入编译阶段.预编译伪指令一般都以#打头,且其前面只能出现空白字符.预编译伪指令不是C++/C ...

  4. The reference to entity "characterEncoding" must end with the ';' delimiter

    数据源配置时加上编码转换格式后出问题了: The reference to entity "characterEncoding" must end with the ';' del ...

  5. Logback相关知识汇总

    例如:%-4relative 表示,将输出从程序启动到创建日志记录的时间 进行左对齐 且最小宽度为4格式修饰符,与转换符共同使用:可选的格式修饰符位于“%”和转换符之间.第一个可选修饰符是左对齐 标志 ...

  6. elk工作原理

    这个配置文件,是读取nginx日志写入到redis zjtest7-redis:/usr/local/logstash-2.3.4/config# cat logstash_agent.conf in ...

  7. C语言 结构体数组保存到二进制文件中

    在项目中我定义了一个结构体数组,头文件如下: C/C++ code   ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 ...

  8. UINavigationController具体解释(二)

    @UINavigationBar-----(是一个View)基本介绍 1.导航栏,和导航控制器一样,是一个容器用来显示提供的其它对象的内容 2.导航栏显示的内容,通过设置UINavigationIte ...

  9. HDU 1862 EXCEL次序 (排序水问题)

    Problem Description Excel对能够记录一组由任意列排序指定.现在,请把你编译的代码类似特征.   Input 測试输入包括若干測试用例. 每一个測试用例的第1行包括两个整数 N ...

  10. iOS 之使用CAShapeLayer中的CAGradientLayer实现圆环的颜色渐变

    本文转载自:http://blog.csdn.net/zhoutao198712/article/details/20864143 在 Github上看到一些进度条的功能,都是通过Core Graph ...