比较数据类型,C#中的值 [英] Compare Datatype, Value in C#

查看:76
本文介绍了比较数据类型,C#中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较两个类的数据类型并返回bool值。问题是我的方法不比较类中的类值



这是代码:



  public   static   class 比较
{
public static bool PublicInstancePropertiesEqual< T>( this T self,T to, params string [] ignore)其中 T: class
{
if (self!= null && to != null
{
var type = typeof运算(T);
var ignoreList = new List< string>(ignore);
var unequalProperties =
来自 pi in type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
where !ignoreList.Contains(pi.Name)
selfValue = type.GetProperty(pi.Name).GetValue(self, null
toValue = type.GetProperty(pi.Name).GetValue(to, null
其中 selfValue!= toValue&& (selfValue == null ||!PublicInstancePropertiesEqual(selfValue,toValue,ignore))
select selfValue;
return !unequalProperties.Any();
}
return self == to;
}
}





这里是比较:



  private   void  Form1_Load( object  sender,EventArgs e)
{
Obj1 obj1 = new Obj1();
Obj1 obj11 = new Obj1();
Obj2 obj2 = new Obj2();
Obj2 obj22 = new Obj2();

obj1.param1 = 1;
obj1.param2 = 2;

obj2.param3 = 3;
obj1.obj2 = obj2;

obj11.param1 = 1;
obj11.param2 = 2;
obj22.param3 = 4;
obj11.obj2 = obj22;
bool res = Compare.PublicInstancePropertiesEqual(obj1,obj11,( 安全));

}
}
class Obj1
{
public string param1 { get ; set ; }
public string param2 { get ; set ; }
public Obj2 obj2 { get ; set ; }
}
class Obj2
{
public string param3 { get ; set ; }
public decimal param4 { get ; set ; }

}





即使参数值发生变化,返回值也为真。 div class =h2_lin>解决方案

首先抛弃LINQ的东西。在正常语句中重写,以便您可以调试正在进行的操作。这是一个开始:

  public   static   bool  PublicInstancePropertiesEqual< t>( this  T self,T to, params   string  [] ignore)其中 T: class  
{
if (self!= null && to!= null
{
var type = < span class =code-keyword> typeof (T);
var ignoreList = new List< string>(ignore);

var typeProperties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

foreach var pi in typeProperties)
{
var property = type.GetProperty(pi.Name);

var selfValue = property.GetValue(self);
var toValue = property.GetValue(to);

if (selfValue!= toValue)
return ;
}

返回 true ;
}

返回 false ;
} < / string > < / t >



另外,你已经写了一个扩展名,所以你不需要指定比较.... 使用它。您需要做的就是:

 bool result = x.PublicInstancePropertiesEqual(y,ignoreList); 


I'd like to compare the data types of two classes and return bool values. The problem is my method doesn't compare values inside class of a class

Here is the code:

public static class Compare
{
    public static bool PublicInstancePropertiesEqual<T>(this T self, T to, params string[] ignore) where T : class
    {
        if (self != null && to != null)
        {
            var type = typeof(T);
            var ignoreList = new List<string>(ignore);
            var unequalProperties =
                from pi in type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                where !ignoreList.Contains(pi.Name)
                let selfValue = type.GetProperty(pi.Name).GetValue(self, null)
                let toValue = type.GetProperty(pi.Name).GetValue(to, null)
                where selfValue != toValue && (selfValue == null || !PublicInstancePropertiesEqual(selfValue, toValue, ignore))
                select selfValue;
            return !unequalProperties.Any();
        }
        return self == to;
    }
}



And here is the comparison:

private void Form1_Load(object sender, EventArgs e)
    {
        Obj1 obj1 = new Obj1();
        Obj1 obj11 = new Obj1();
        Obj2 obj2 = new Obj2();
        Obj2 obj22 = new Obj2();

        obj1.param1 = "1";
        obj1.param2 = "2";

        obj2.param3 = "3";
        obj1.obj2 = obj2;

        obj11.param1 = "1";
        obj11.param2 = "2";
        obj22.param3 = "4";
        obj11.obj2 = obj22;
        bool res = Compare.PublicInstancePropertiesEqual(obj1, obj11, ("secure"));

    }
}
class Obj1
{
    public string param1 { get; set; }
    public string param2 { get; set; }
    public Obj2 obj2 { get; set; }
}
class Obj2
{
    public string param3 { get; set; }
    public decimal param4 { get; set; }

}



The returned value is res is true even when the values of the parameters change.

解决方案

Start by ditching the LINQ stuff. Rewrite in normal statements so you can debug what's going on. Here's a start:

public static bool PublicInstancePropertiesEqual<t>(this T self, T to, params string[] ignore) where T : class
        {
            if (self != null && to != null)
            {
                var type = typeof(T);
                var ignoreList = new List<string>(ignore);
                
                var typeProperties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

                foreach (var pi in typeProperties)
                {
                    var property = type.GetProperty(pi.Name);

                    var selfValue = property.GetValue(self);
                    var toValue = property.GetValue(to);

                    if (selfValue != toValue)
                        return false;
                }

                return true;
            }

            return false;
        }</string></t>


Also, you've written an extension so you don't need to specify the Compare.... to use it. All you need to do is:

bool result = x.PublicInstancePropertiesEqual(y, ignoreList);


这篇关于比较数据类型,C#中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆