/// <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. NET Core,你必须了解无处不在的“依赖注入”

    NET Core,你必须了解无处不在的“依赖注入” ASP.NET Core的核心是通过一个Server和若干注册的Middleware构成的管道,不论是管道自身的构建,还是Server和Middle ...

  2. elk工作原理

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

  3. java学习之xml

    xml的处理有两种方式dom和Sax 其中dom有3套api ,分别是dom和jdom和dom4j package com.gh.xml; import java.io.File; import ja ...

  4. Card Game Cheater(贪心+二分匹配)

    Card Game Cheater Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  5. Unity KillCount

    using UnityEngine; using System.Collections; public class KillCountMult : MonoBehaviour { public GUI ...

  6. ThinkPHP - 模板使用函数

    模板使用函数 1.模板引擎自带函数:仅仅是输出变量并不能满足模板输出的需要,内置模板引擎支持对模板变量使用调节器和格式化功能,其实也就是提供函数支持,并支持多个函数同时使用.用于模板标签的函数可以是P ...

  7. 驱动: 中断【1】linux中断流程

    通常情况下,当一个给定的中断处理程序正在执行时,所有其他的中断都是打开的,所以这些不同中断线上的其他中断都能被处理,但当前中断总是被禁止的. 将中断处理切为两个部分或两半.中断处理程序上半部(top ...

  8. hibernate报错

    报错二:java.lang.ExceptionInInitializerError java.lang.ExceptionInInitializerError at com.java1234.serv ...

  9. linux下java窗口,正确显示中文

    Tip1 1.在 JAVA_HOME/jre/lib/fonts/ 下建立个目录 fallback 2.在 fallback 里弄个中文字体最简单ln一下就好了 比如: ln -s /usr/shar ...

  10. php函数参数

    函数的参数 通过参数列表可以传递信息到函数,即以逗号作为分隔符的表达式列表.参数是从左向右求值的. PHP 支持按值传递参数(默认),通过引用传递参数以及默认参数.也支持可变长度参数列表,更多信息参见 ...