当!=被选中时,2个相同类型的对象返回true

时间:2018-08-29 14:09:47

标签: winforms oop c#-4.0

我正在将model.GetType()。GetProperties()与foreach用来比较同一类的2个对象的属性。 像这样

 foreach (var item in kayit.GetType().GetProperties())
                    {
                        var g = item.GetValue(plu);
                        var b = item.GetValue(kayit);
                        if (g is string && b is string&& g!=b)
                        {
                                a += item.Name + "*";

                        }
                        else if (g is DateTime&& b is DateTime&& g!=b)
                        {
                            a += item.Name + "*";
                        }


                    }

但是问题是即使它们具有相同的值g!= b始终返回true。我已经使用一个断点来证明这一点,并且它们实际上是同一件事。实际上,我采用的是将其放在文本框中的值,然后在单击按钮后创建另一个类,并整理以查看更改的属性。因此,即使我不进行任何更改,它也不会读取mas等于。有人可以帮我吗?

更多信息: 我从数据库获取了plu,并用它填充控件:

            txtorder.Text = plu.OrderNo;
            dtporder.Value = nulldate(plu.OrderDate);            
            dtp1fit.Value = nulldate(plu.FirstFitDate);
            dtp1yorum.Value = nulldate(plu.FirstCritDate);
            dtp2fit.Value = nulldate(plu.SecondFitDate);
            dtp2yorum.Value = nulldate(plu.SecondCritDate);
            dtpsizeset.Value = nulldate(plu.SizeSetDate);
            dtpsizesetok.Value = nulldate(plu.SizeSetOkDate);
            dtpkumasplan.Value = nulldate(plu.FabricOrderByPlan);
            txtTedarikci.Text = plu.Fabric_Supplier;
            dtpkumasFP.Value = nulldate(plu.FabricOrderByFD);
            dtpfabarrive.Value = nulldate(plu.FabricArrive);
            dtpbulk.Value = nulldate(plu.BulkFabricDate);
            dtpbulkok.Value = nulldate(plu.BulkConfirmDate);
            dtpaccessory.Value = nulldate(plu.AccessoriesDate);
            dtpaccessoryarrive.Value = nulldate(plu.AccessoriesArriveDate);
            dtpcutok.Value = nulldate(plu.ProductionStartConfirmation);
            dtpcutstart.Value = nulldate(plu.ProductionStart);
            dtpshipmentdate.Value = nulldate(plu.ShipmentDate);
            dtpshipmentsample.Value = nulldate(plu.ShipmentSampleDate);
            dtpshippedon.Value = nulldate(plu.Shippedon);

nulldate只是将空值更改为默认值的一种方法。 这是单击按钮后的操作:

 var kayit = new uretim();
  kayit.OrderNo = txtorder.Text.ToUpper();
                kayit.OrderDate = vdat(dtporder.Value);
                kayit.FirstFitDate = vdat(dtp1fit.Value);
                kayit.FirstCritDate = vdat(dtp1yorum.Value);
                kayit.SecondFitDate = vdat(dtp2fit.Value);
                kayit.SecondCritDate = vdat(dtp2yorum.Value);
                kayit.SizeSetDate = vdat(dtpsizeset.Value);
                kayit.SizeSetOkDate = vdat(dtpsizesetok.Value);
                kayit.FabricOrderByPlan = vdat(dtpkumasplan.Value);
                kayit.Fabric_Supplier = txtTedarikci.Text;
                kayit.FabricOrderByFD = vdat(dtpkumasFP.Value);
                kayit.FabricArrive = vdat(dtpfabarrive.Value);
                kayit.BulkFabricDate = vdat(dtpbulk.Value);
                kayit.BulkConfirmDate = vdat(dtpbulkok.Value);
                kayit.AccessoriesDate = vdat(dtpaccessory.Value);
                kayit.AccessoriesArriveDate = vdat(dtpaccessoryarrive.Value);
                kayit.ProductionStartConfirmation = vdat(dtpcutok.Value);
                kayit.ProductionStart = vdat(dtpcutstart.Value);
                kayit.ShipmentDate = vdat(dtpshipmentdate.Value);
                kayit.ShipmentSampleDate = vdat(dtpshipmentsample.Value);
                kayit.Shippedon = vdat(dtpshippedon.Value);
                kayit.Status = true;
                kayit.WrittenDate = DateTime.Now;
                kayit.GuidKey = plu.GuidKey != null ? plu.GuidKey : Guid.NewGuid().ToString("N");

我已经通过断点证明了值实际上是相同的。但是!=检查会重新运行。

1 个答案:

答案 0 :(得分:1)

做事时

g != b

编译器不知道这些对象是要比较的字符串,因此它比较它们的引用。您可以这样做:

g.Equals(b) //be carefull if one of them is null

g.ToString() != b.ToString()

编辑

您可以在检查类型后进行比较:

if (g is string && b is string)
{
    if( g.ToString() != b.ToString() ){

    }                    

}
相关问题