zoukankan      html  css  js  c++  java
  • 用反射,将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);
                }
                return 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))))));
            }
        }

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

  • 相关阅读:
    《入门经典》——6.15
    《Linear Algebra and Its Application》-chaper1-行化简法解决线性方程组
    《算法实战策略》-chaper19-队列、栈和双端队列
    《Linear Algebra and Its Applications》-chaper1-向量方程、矩阵方程和线性方程组
    辛普森法则
    《训练指南》——6.15
    《入门经典》——6.21
    《算法问题实战策略》-chaper13-数值分析
    Scheme 中的 pair 和 list 简述
    C. Friends
  • 原文地址:https://www.cnblogs.com/LJP-JumpAndFly/p/5031110.html
Copyright © 2011-2022 走看看