zoukankan      html  css  js  c++  java
  • xml 转换成对象(采用反射机制对对对象属性赋值)

           /// <summary>
            /// 采用反射机制对对对象属性赋值
            /// </summary>
            /// <param name="node"></param>
            /// <param name="P"></param>
            public static void ReflexValue(XmlNode Nodes, object obj)
            {
                XmlNodeList nodeList = Nodes.ChildNodes;
                foreach (XmlNode node in nodeList)
                {
    
                    string filedName = node.Name.ToUpper();
                    try
                    {
                        //取到属性
                        PropertyInfo property = obj.GetType().GetProperty(filedName);
                        var valtype = property.PropertyType;
                        if (valtype.IsGenericType && valtype.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))//判断convertsionType是否为nullable泛型类  
                        {
                            //如果type为nullable类,声明一个NullableConverter类,该类提供从Nullable类到基础基元类型的转换  
                            System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(valtype);
                            //将type转换为nullable对的基础基元类型  
                            valtype = nullableConverter.UnderlyingType;
                        }
                        var val = node.InnerText;
                        //decimal dl = 0;
                        DateTime dt;
                        //if (valtype.Name == "Decimal" || valtype.Name == "Single")
                        //{
                        //    if (decimal.TryParse(val, out  dl))
                        //        val = dl.ToString();
                        //}
                        //else if (valtype.Name == "DateTime")
                        //{
                        //    if (DateTime.TryParse(val, out  dt))
                        //        val = dt.ToString();
                        //    else
                        //    {
                        //        val = DateTime.Now.ToString();
                        //    }
                        //}
                        if (valtype.Name == "DateTime")
                        {
    
                            if (DateTime.TryParseExact(val, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None, out dt))
                                val = dt.ToString();
                            else
                            {
                                val = DateTime.Now.ToString();
                            }
                        }
                        if (valtype.Name != "String" && val == "")
                            property.SetValue(obj, null);
                        else
                            property.SetValue(obj, Convert.ChangeType(val, valtype), null);
                    }
                    catch (Exception ex)
                    {
                        Common.WriteExceptionLog(ex);
                    }
                }
            }
  • 相关阅读:
    无题
    无题
    Windows NT 和 VMS: 其余的故事 (The Rest of the Story)
    Lachesis Shield Released!
    最近几年来看到的最强的照片
    有关 Nintendo GameCube
    那些带给我欢乐的游戏
    习惯了 .#
    Rootkits
    我写的IDA插件发布了
  • 原文地址:https://www.cnblogs.com/-hao/p/9935344.html
Copyright © 2011-2022 走看看