/// <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. Iterator(迭代器)接口 --对象循环遍历

    <?php class MyIterator implements Iterator { private $var = array(); public function __construct ...

  2. Hadoop Hive与Hbase整合+thrift

    Hadoop Hive与Hbase整合+thrift 1.  简介 Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供完整的sql查询功能,可以将sql语句 ...

  3. C# 操作Excel (二)

    根据翻阅LTP.Net知识库系列之四中Office操作功能: 一.记录常用如下 (1)“在自己的程序中宿主Office”的意思就是在我们自己开发的应用程序的窗体上,显示一个就像Office应用程序那样 ...

  4. CoreAnimation (CALayer 动画)

    CoreAnimation基本介绍: CoreAnimation动画位于iOS框架的Media层 CoreAnimation动画实现需要添加QuartzCore.Framework CoreAnima ...

  5. SQL 表连接,内联、外联、全连

    内连接,join 或 inner join 两个表中符合条件的集合 外连接,left join  或  right  join 以left左边或right右边的表为数据集合行,根据条件,另一侧没有的数 ...

  6. HDU 4720 Naive and Silly Muggles (外切圆心)

    Naive and Silly Muggles Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...

  7. poj1742 多重背包的可行性问题

    http://poj.org/problem? id=1742 Description People in Silverland use coins.They have coins of value ...

  8. C#中的DataTable简单使用Merge

    //不同结构的DataTable追加第二个DataTable数据在对应行后的 简单使用//不同结构的DataTable追加在行后面的合并 DataTable dt = new DataTable(); ...

  9. 【JQuery Plugin】WdatePicker

    <div class="timeSelect reportDate"> <span>查询时间:</span> <input type=&q ...

  10. c++特殊函数

    C++中NULL不能写作小写,NULL的值为零,也可以写作0 在自己写的复制构造函数中不改变原对象,所以传进来的参数可以设为const类型的,这样可以保证传进来的对象不被改变 比如A(const A ...