我创建个实例化对象,只有满足条件才赋值,我实现的是只赋给一些值不是对象中所有都赋值。如果能判断有几个赋值的方法最好了,别告诉一一比较。在线等。不懂的留言,我随时都在线。

解决方案 »

  1.   

    就是想判断对象中是否有值!
    如:
    User us = new User();
    is(条件)
        us.loginId = 1;
    if(如果User对象有被赋值)
        做相应的操作!
    否则不做操作。看得懂吗?
      

  2.   

    User us = new User();
    初始的时候干了什么?
      

  3.   

    手写的代码,自己完善下。
    bool CompareObj<T>(T obj1, T obj2)
    {
        return typeof(T).GetPropertites().All(x => x.GetValue(obj1, null) != x.GetValue(obj2, null);
    }T CloneObj<T>(T source)
    {
        T newobj = new T();
        typeof(T).GetPropertites().ToList()
            .ForEach(x => x.SetValue(newobj, x.GetValue(source, null), null));
        return newobj;
    }使用:
    User us = new User();
    oldus = CloneObj(us);
    is(条件)
      us.loginId = 1;
    if(!CompareObj(oldus, us))
      做相应的操作!
    否则不做操作。
      

  4.   

    bool CompareObj<T>(T obj1, T obj2)
    {
        return typeof(T).GetPropertites().All(x => x.GetValue(obj1, null) != x.GetValue(obj2, null);
    }修改下bool CompareObj<T>(T obj1, T obj2)
    {
        return typeof(T).GetPropertites().All(x => x.GetValue(obj1, null) == x.GetValue(obj2, null);
    }当然,你也可以只监控指定的属性bool CompareObj<T>(T obj1, T obj2, string[] CompareThisPropertiesOnly)
    {
        return typeof(T)
                   .GetPropertites()
                   .Where(x => CompareThisPropertiesOnly.Any(y => y == x.Name))
                   .All(x => x.GetValue(obj1, null) == x.GetValue(obj2, null);
    }使用:
    CompareObj(user1, user2, new string[] { "Name", "Age", "Sex", "Email", "Address" });