コード例 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="row"></param>
        /// <param name="alias">alias da tabela</param>
        /// <returns></returns>
        public static T ToObject <T>(this DataRow row, List <string> alias = null) where T : class, new()
        {
            T             obj     = new T();
            List <string> colunas = new List <string>();

            foreach (var p in obj.GetType().GetProperties())
            {
                try
                {
                    if (p.PropertyType.IsClass && !SYSinfo.EumTipoPrimitivo(p.PropertyType))
                    {
                        string fname  = obj.GetType().FullName;
                        string bfname = obj.GetType().BaseType.FullName;
                        if (p.PropertyType.FullName != fname && p.PropertyType.FullName != bfname)
                        {
                            toSubOjbject <T>(obj, p, row, alias);
                        }
                    }
                    else
                    {
                        string pName;
                        bool   achou = getColumnNameForProperty(p, out pName, row, alias);
                        if (achou)
                        {
                            var val = row[pName];
                            if (p.PropertyType.IsGenericType && p.PropertyType.Name.Contains("Nullable"))
                            {
                                if (!string.IsNullOrEmpty(val.ToString()))
                                {
                                    //object objnullable = Convert.ChangeType(obj, Nullable.GetUnderlyingType(p.PropertyType) ?? p.PropertyType);
                                    object objnullable = Convert.ChangeType(val, Nullable.GetUnderlyingType(p.PropertyType) ?? p.PropertyType);
                                    p.SetValue(obj, objnullable, null);
                                }
                            }
                            else if (p.PropertyType.BaseType == typeof(Enum))
                            {
                                foreach (var el in Enum.GetValues(p.PropertyType))
                                {
                                    int venum = (int)el;
                                    if (val.Equals(el.ToString()) || val.Equals(venum))
                                    {
                                        p.SetValue(obj, Convert.ChangeType(el, p.PropertyType), null);
                                        break;
                                    }
                                }
                            }
                            else if (val != null && val != DBNull.Value)
                            {
                                p.SetValue(obj, Convert.ChangeType(val, p.PropertyType), null);
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    continue;
                }
            }
            return(obj);
        }
コード例 #2
0
ファイル: Dbo.cs プロジェクト: megahacktime/Time30
        public object getDbValue <T>(T item, string propertyName = null)
        {
            object value = DBNull.Value;

            if (SYSinfo.EumTipoPrimitivo(typeof(T)))
            {
                if (item != null)
                {
                    value = item;
                }
            }
            else if (propertyName != null) // demais tipos
            {
                PropertyInfo[] pinfos = typeof(T).GetProperties();
                var            pinfo  = pinfos.First(p => p.Name == propertyName);
                if (pinfo != null)
                {
                    value = pinfo.GetValue(item);
                }
            }
            return(value);
        }