예제 #1
0
        public static bool ObjectFromDataRow(object data, DataRow dr, bool include_underscores = false)
        {
            bool result   = true;
            Type dataType = data.GetType();

            PropertyInfo[] arrPropertyInfos = dataType.GetProperties();
            foreach (PropertyInfo dataPropertyInfo in arrPropertyInfos)
            {
                Type   tmpType = dataPropertyInfo.PropertyType;
                string tmpName = dataPropertyInfo.Name;
                if (tmpName.StartsWith("_") && (!include_underscores))
                {
                    continue;
                }
                if (!dr.Table.Columns.Contains(tmpName))
                {
                    continue;
                }
                if (tmpName.StartsWith("sync_ts"))
                {
                    dataPropertyInfo.SetValue(data, GConv.DbTsToLong(dr[tmpName]), null);
                    continue;
                }
                if (tmpType == typeof(string))
                {
                    dataPropertyInfo.SetValue(data, GConv.DbToStr(dr[tmpName]), null);
                }
                else if (tmpType == typeof(bool))
                {
                    dataPropertyInfo.SetValue(data, GConv.DbToBln(dr[tmpName]), null);
                }
                else if (tmpType == typeof(Byte))
                {
                    dataPropertyInfo.SetValue(data, GConv.DbToByte(dr[tmpName]), null);
                }
                else if (tmpType == typeof(Byte?))
                {
                    dataPropertyInfo.SetValue(data, GConv.DbToByteNull(dr[tmpName]), null);
                }
                else if (tmpType == typeof(Int16))
                {
                    dataPropertyInfo.SetValue(data, GConv.DbToShort(dr[tmpName]), null);
                }
                else if (tmpType == typeof(Int16?))
                {
                    dataPropertyInfo.SetValue(data, GConv.DbToShortNull(dr[tmpName]), null);
                }
                else if (tmpType == typeof(Int32))
                {
                    dataPropertyInfo.SetValue(data, GConv.DbToInt(dr[tmpName]), null);
                }
                else if (tmpType == typeof(Int32?))
                {
                    dataPropertyInfo.SetValue(data, GConv.DbToIntNull(dr[tmpName]), null);
                }
                else if (tmpType == typeof(Int64))
                {
                    dataPropertyInfo.SetValue(data, GConv.DbToLong(dr[tmpName]), null);
                }
                else if (tmpType == typeof(Int64?))
                {
                    dataPropertyInfo.SetValue(data, GConv.DbToLongNull(dr[tmpName]), null);
                }
                else if (tmpType == typeof(Single))
                {
                    dataPropertyInfo.SetValue(data, GConv.DbToFloat(dr[tmpName]), null);
                }
                else if (tmpType == typeof(Single?))
                {
                    dataPropertyInfo.SetValue(data, GConv.DbToFloatNull(dr[tmpName]), null);
                }
                else if (tmpType == typeof(Double))
                {
                    dataPropertyInfo.SetValue(data, GConv.DbToDbl(dr[tmpName]), null);
                }
                else if (tmpType == typeof(Double?))
                {
                    dataPropertyInfo.SetValue(data, GConv.DbToDblNull(dr[tmpName]), null);
                }
                else if (tmpType == typeof(Decimal))
                {
                    dataPropertyInfo.SetValue(data, GConv.DbToDec(dr[tmpName]), null);
                }
                else if (tmpType == typeof(Decimal?))
                {
                    dataPropertyInfo.SetValue(data, GConv.DbToDecNull(dr[tmpName]), null);
                }
                else if (tmpType == typeof(DateTime))
                {
                    dataPropertyInfo.SetValue(data, GConv.DbToDt(dr[tmpName]), null);
                }
                else if (tmpType == typeof(DateTime?))
                {
                    dataPropertyInfo.SetValue(data, GConv.DbToDtNull(dr[tmpName]), null);
                }
                else if (tmpType == typeof(Guid))
                {
                    dataPropertyInfo.SetValue(data, GConv.DbToGid(dr[tmpName]), null);
                }
                else
                {
                    throw new Exception("ObjectFromDataRow type not supported!");
                }
            }
            return(result);
        }