通过反射初始化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);
        }

 

posted @ 2013-04-02 15:27  小さいです哥  阅读(367)  评论(0)    收藏  举报