zoukankan      html  css  js  c++  java
  • 通过反射初始化Class值【原】

    public class TbRoute
            {
                private string _routeid;
                private string _routenm;
                private bool _flag;
                private int _count;
    
                public string RouteId
                {
                    set { _routeid = value; }
                    get { return _routeid; }
                }
    
                public string RouteNM
                {
                    set { _routenm = value; }
                    get { return _routenm; }
                }
    
                public bool Flag
                {
                    set { _flag = value; }
                    get { return _flag; }
                }
    
                public int Count
                {
                    set { _count = value; }
                    get { return _count; }
                }
            }
    
            private static object InitClassValue(object obj, List<string[]> listValues)
            {
                Type t = obj.GetType();//获得该类的Type
                //再用Type.GetProperties获得PropertyInfo[],然后就可以用foreach 遍历了
                foreach (PropertyInfo pi in t.GetProperties())
                {
                    for (int i = 0; i < listValues.Count; i++)
                    {
                        string[] values = (string[])listValues[i];
                        if (values.Length.Equals(2))
                        {
                            if (values[0].ToLower().Equals(pi.Name.ToLower()))
                            {
                                object value = GetDbTypeValue(pi.PropertyType, values[1]);
                                pi.SetValue(obj, value, null);
                                break;
                            }
                        }
                    }
                }
                return obj;
            }
    
            private static object GetDbTypeValue(Type type, object obj)
            {
                object value = "";
                if (type.Equals(typeof(int)) || type.IsEnum)
                    try
                    {
                        value = Int32.Parse(obj.ToString());
                    }
                    catch
                    {
                        value = 0;
                    }
                else if (type.Equals(typeof(long)))
                    try
                    {
                        value = Int32.Parse(obj.ToString());
                    }
                    catch
                    {
                        value = 0;
                    }
                else if (type.Equals(typeof(double)) || type.Equals(typeof(Double)) || type.Equals(typeof(Single)))
                    try
                    {
                        value = decimal.Parse(obj.ToString());
                    }
                    catch
                    {
                        value = 0.0;
                    }
                else if (type.Equals(typeof(DateTime)))
                    try
                    {
                        value = DateTime.Parse(obj.ToString());
                    }
                    catch
                    {
                        value = DateTime.MinValue;
                    }
                else if (type.Equals(typeof(bool)))
                    try
                    {
                        value = Boolean.Parse(obj.ToString());
                    }
                    catch
                    {
                        value = false;
                    }
                else if (type.Equals(typeof(string)))
                    try
                    {
                        value = obj.ToString();
                    }
                    catch
                    {
                        value = "";
                    }
                else if (type.Equals(typeof(decimal)))
                    try
                    {
                        value = decimal.Parse(obj.ToString());
                    }
                    catch
                    {
                        value = 0.0;
                    }
                else if (type.Equals(typeof(byte[])))
                    try
                    {
                        value = (byte[])obj;
                    }
                    catch 
                    { 
                        value= new byte[]{};
                    }
                else if (type.Equals(typeof(Guid)))
                    try
                    {
                        value = obj.ToString();
                    }
                    catch
                    {
                        value = "";
                    }
                return value;
            }
    
            private void FormTest_Load(object sender, EventArgs e)
            {
                List<string[]> listValues = new List<string[]>();
                listValues.Add(new string[] { "RouteId", "10000" });
                listValues.Add(new string[] { "RouteNM", "xsm" });
    
    
    
    
                string ss = "[[\"RouteId\",\"10000\"],[\"RouteNM\",\"xsm\"],[\"Flag\",\"true\"],[\"Count\",\"1\"]]";
    
                listValues = JsonHelp.JSONToObject<List<string[]>>(ss);
    
                TbRoute t = new TbRoute();
                t = (TbRoute)InitClassValue(t, listValues);
            }
  • 相关阅读:
    InnoDB存储引擎介绍-(2)redo和undo学习
    InnoDB存储引擎介绍-(1)InnoDB存储引擎结构
    MySQL共享表空间概念
    MySQL压力测试(1)-mysqlslap
    MySQL5.6复制技术(4)-MySQL主从复制过滤参数
    MySQL5.6复制技术(3)-MySQL主从复制线程状态转变
    MySQL5.6复制技术(2)-主从部署以及半同步配置详细过程
    vue 子组件 $emit方法 调用父组件方法
    es聚合后排序
    java比较两个小数的大小
  • 原文地址:https://www.cnblogs.com/xsmhero/p/2995593.html
Copyright © 2011-2022 走看看