Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TElement"></typeparam>
        /// <param name="tb"></param>
        /// <returns></returns>
        public static List <TElement> TableConvertToList <TElement>(DataTable tb) where TElement : new()
        {
            List <TElement> list         = new List <TElement>();
            var             propertyList = typeof(TElement).GetProperties().Where(x => x.CanWrite);

            if (tb != null && tb.Columns.Count > 0 && tb.Rows.Count > 0)
            {
                for (int i = 0; i < tb.Rows.Count; i++)
                {
                    TElement element = new TElement();
                    var      row     = tb.Rows[i];
                    for (int j = 0; j < tb.Columns.Count; j++)
                    {
                        try
                        {
                            var columnName = tb.Columns[j].ColumnName;
                            var property   =
                                propertyList.FirstOrDefault(
                                    x => string.Equals(columnName, x.Name, StringComparison.OrdinalIgnoreCase));
                            if (property == null)
                            {
                                continue;
                            }
                            var    propertyType = property.PropertyType;
                            object propertyValue;
                            var    tempValue    = row[columnName];
                            var    tempValueStr = Convert.ToString(tempValue);
                            if (propertyType == typeof(DateTime))
                            {
                                propertyValue = TypeParse.StrToDateTime(tempValue, DateTime.Now);
                            }
                            else if (propertyType == typeof(DateTime?))
                            {
                                DateTime temp;
                                if (!string.IsNullOrEmpty(tempValueStr) && DateTime.TryParse(tempValueStr, out temp))
                                {
                                    propertyValue = (DateTime?)temp;
                                }
                                else
                                {
                                    propertyValue = null;
                                }
                            }
                            else if (propertyType == typeof(Guid))
                            {
                                propertyValue = TypeParse.StrToGuid(tempValue);
                            }
                            else if (propertyType == typeof(Guid?))
                            {
                                Guid temp;
                                if (!string.IsNullOrEmpty(tempValueStr) && Guid.TryParse(tempValueStr, out temp))
                                {
                                    propertyValue = (Guid?)temp;
                                }
                                else
                                {
                                    propertyValue = null;
                                }
                            }
                            else if (propertyType == typeof(Double?))
                            {
                                Double temp;
                                if (!string.IsNullOrEmpty(tempValueStr) && Double.TryParse(tempValueStr, out temp))
                                {
                                    propertyValue = (Double?)temp;
                                }
                                else
                                {
                                    propertyValue = null;
                                }
                            }
                            else if (propertyType == typeof(int?))
                            {
                                int temp;
                                if (!string.IsNullOrEmpty(tempValueStr) && int.TryParse(tempValueStr, out temp))
                                {
                                    propertyValue = (int?)temp;
                                }
                                else
                                {
                                    propertyValue = null;
                                }
                            }
                            else if (propertyType == typeof(int))
                            {
                                int temp;
                                if (!string.IsNullOrEmpty(tempValueStr) && int.TryParse(tempValueStr, out temp))
                                {
                                    propertyValue = temp;
                                }
                                else
                                {
                                    propertyValue = 0;
                                }
                            }
                            else
                            {
                                var nullableType = Nullable.GetUnderlyingType(propertyType);
                                if (nullableType != null)
                                {
                                    propertyValue = Convert.ChangeType(tempValueStr, nullableType);
                                }
                                else
                                {
                                    propertyValue = Convert.ChangeType(tempValueStr, propertyType);
                                }
                            }
                            property.SetValue(element, propertyValue, new object[] { });
                        }
                        catch (Exception e) { }
                    }
                    list.Add(element);
                }
            }
            return(list);
        }
        /// <summary>
        /// 根据属性名称-值列表设置对象值
        /// </summary>
        /// <typeparam name="TElement"></typeparam>
        /// <param name="obj"></param>
        /// <param name="propertyNameValue"></param>
        /// <returns></returns>
        public static void SetValue <TElement>(TElement obj, IEnumerable <KeyValuePair <string, string> > propertyNameValue)
            where TElement : class
        {
            if (obj == null)
            {
                return;
            }
            var elementType = typeof(TElement);

            foreach (var item in propertyNameValue)
            {
                try
                {
                    if (item.Value == null)
                    {
                        continue;
                    }
                    var property = elementType.GetProperty(item.Key);
                    if (property == null)
                    {
                        continue;
                    }
                    var    propertyType = property.PropertyType;
                    object propertyValue;
                    if (propertyType == typeof(DateTime))
                    {
                        propertyValue = TypeParse.StrToDateTime(item.Value, DateTime.Now);
                    }
                    else if (propertyType == typeof(DateTime?))
                    {
                        DateTime temp;
                        if (!string.IsNullOrEmpty(item.Value) && DateTime.TryParse(item.Value, out temp))
                        {
                            propertyValue = (DateTime?)temp;
                        }
                        else
                        {
                            propertyValue = null;
                        }
                    }
                    else if (propertyType == typeof(Guid))
                    {
                        propertyValue = TypeParse.StrToGuid(item.Value);
                    }
                    else if (propertyType == typeof(Guid?))
                    {
                        Guid temp;
                        if (!string.IsNullOrEmpty(item.Value) && Guid.TryParse(item.Value, out temp))
                        {
                            propertyValue = (Guid?)temp;
                        }
                        else
                        {
                            propertyValue = null;
                        }
                    }
                    else if (propertyType == typeof(Double?))
                    {
                        Double temp;
                        if (!string.IsNullOrEmpty(item.Value) && Double.TryParse(item.Value, out temp))
                        {
                            propertyValue = (Double?)temp;
                        }
                        else
                        {
                            propertyValue = null;
                        }
                    }
                    else if (propertyType == typeof(int?))
                    {
                        int temp;
                        if (!string.IsNullOrEmpty(item.Value) && int.TryParse(item.Value, out temp))
                        {
                            propertyValue = (int?)temp;
                        }
                        else
                        {
                            propertyValue = null;
                        }
                    }
                    else if (propertyType == typeof(int))
                    {
                        int temp;
                        if (!string.IsNullOrEmpty(item.Value) && int.TryParse(item.Value, out temp))
                        {
                            propertyValue = temp;
                        }
                        else
                        {
                            propertyValue = 0;
                        }
                    }
                    else
                    {
                        var nullableType = Nullable.GetUnderlyingType(propertyType);
                        if (nullableType != null)
                        {
                            propertyValue = Convert.ChangeType(item.Value, nullableType);
                        }
                        else
                        {
                            propertyValue = Convert.ChangeType(item.Value, propertyType);
                        }
                    }
                    property.SetValue(obj, propertyValue, new object[] { });
                }
                catch (Exception e)
                {
                }
            }
        }