获取对象属性值

public static List<TBS.Model.FieldUpdate> CheckUpdateField(object o1, object o2, ref int result)
        {
            Type type1 = o1.GetType();
            Type type2 = o2.GetType();
            if (type1 != type2)
            {
                result = -1;
                return null;
            }

            else
            {
                List<TBS.Model.FieldUpdate> fieldUpdateList = new List<TBS.Model.FieldUpdate>();
                System.Reflection.PropertyInfo[] fields1 = type1.GetProperties();
                System.Reflection.PropertyInfo[] fields2 = type2.GetProperties();
                int i = 1;
                int id = 0;
                result = 0;
                foreach (System.Reflection.PropertyInfo field1 in fields1)
                {
                    string name1 = field1.Name;
                    System.Reflection.PropertyInfo pi1 = type1.GetProperty(name1);
                    System.Reflection.PropertyInfo pi2 = type2.GetProperty(name1);

                    object s1 = pi1.GetValue(o1, null);
                    object s2 = pi2.GetValue(o2, null);

                    if (i == 1)
                        id = int.Parse(s1.ToString());
                    i = i + 1;
                    if (!s1.ToString().Equals(s2.ToString()))
                    {
                        TBS.Model.FieldUpdate modelFieldUpdate = new TBS.Model.FieldUpdate();
                        int start = o1.ToString().LastIndexOf('.') + 1;
                        int end = o1.ToString().Length - o1.ToString().LastIndexOf('.') - 1;
                        modelFieldUpdate.TableName = o1.ToString().Substring(start, end);
                        modelFieldUpdate.Field = name1;
                        modelFieldUpdate.RelationID = id;
                        modelFieldUpdate.State = 0;
                        modelFieldUpdate.UpdateValue = s2.ToString();
                        modelFieldUpdate.OriginalValue = s1.ToString();
                        modelFieldUpdate.DataType = s1.GetType().ToString();
                        fieldUpdateList.Add(modelFieldUpdate);
                        result = 1;
                    }
                }
                return fieldUpdateList;
            }
        }

        public static TBS.Model.Ticket FieldToModel(List<TBS.Model.FieldUpdate> fieldUpdates,TBS.Model.Ticket originalTicket)
        {
           
            object model = originalTicket.GetCopy();
            Type modelType = model.GetType();
            foreach(TBS.Model.FieldUpdate fieldUpdate in fieldUpdates)
            {
                Type type = Type.GetType(fieldUpdate.DataType);
                string typestr = type.Name;
                object field = new object();
                if (typestr.ToLower() == "string")
                    field = fieldUpdate.UpdateValue;
                if (typestr.ToLower() == "int32")
                    field = int.Parse(fieldUpdate.UpdateValue);
                if (typestr.ToLower() == "decimal")
                    field = decimal.Parse(fieldUpdate.UpdateValue);
                if (typestr.ToLower() == "datetime")
                    field = DateTime.Parse(fieldUpdate.UpdateValue);
                System.Reflection.PropertyInfo p = modelType.GetProperty(fieldUpdate.Field);
                p.SetValue(model, field, null);
            }
            return (TBS.Model.Ticket)model;
        }

你可能感兴趣的:(对象)