比较某些道具具有不同格式的对象

发布于 2025-01-01 04:53:08 字数 847 浏览 3 评论 0原文

比较两个对象的所有属性的最佳方法是什么,其中一些对象具有不同的格式(例如,一个对象中的 DateTime ,另一个对象中的 DateTime.ToString() 具有自定义格式) ?

我能够通过使用 2 个断言来做到这一点:

o1.ShouldHave().AllPropertiesBut(dto1 => dto1.Date).EqualTo(o2);
o1.Date.Should().Be(DateTime.Parse(o2.Date));

我会考虑以下内容,但这不会编译,因为 EqualTo() 是无效的。

o1.ShouldHave().AllProperties().But(d => d.Date).EqualTo(o2)
.And.Date.Should().Be((DateTime.Parse(o2.Date));

类型有:

public class Dto1
{
    public int ID { get { return 1; } }
    public DateTime Date { get { return DateTime.Now.Date; } }
}

public class Dto2
{
    public int ID { get { return 1; } }
    public string Date { get { return DateTime.Now.Date.ToShortDateString(); } }
}

var o1 = new Dto1();
var o2 = new Dto2();

What is the best way to compare all properties of two objects where some of them have different formats (e.g. DateTime in one and DateTime.ToString() with custom format in other)?

I was able to do that by using 2 assertions:

o1.ShouldHave().AllPropertiesBut(dto1 => dto1.Date).EqualTo(o2);
o1.Date.Should().Be(DateTime.Parse(o2.Date));

I would think about the following, but that does not compile because EqualTo<T>() is void.

o1.ShouldHave().AllProperties().But(d => d.Date).EqualTo(o2)
.And.Date.Should().Be((DateTime.Parse(o2.Date));

types are:

public class Dto1
{
    public int ID { get { return 1; } }
    public DateTime Date { get { return DateTime.Now.Date; } }
}

public class Dto2
{
    public int ID { get { return 1; } }
    public string Date { get { return DateTime.Now.Date.ToShortDateString(); } }
}

var o1 = new Dto1();
var o2 = new Dto2();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心意如水 2025-01-08 04:53:08

第一个例子通常是最好的方法。但是,如果您要切换 o1 和 o2,则可能会在一次调用中起作用。 Fluent Assertions 将尝试将属性的实际值转换(使用 Convert.ChangeType)为同名属性的预期值。在您的特定示例中,它会在比较值之前尝试将 Dto1 中的日期时间转换为 Dto2 中的字符串。但由于 DateTime 的字符串表示形式取决于线程的区域性,因此它不会为您提供可预测的结果。但是,如果您要切换 o1 和 o2,如果 Convert.ChangeType 成功将您的短日期时间转换回 DateTIme 对象,我不会感到惊讶。

顺便说一句,我的 DTO 通常只是将 DateTime 传递给调用者,而不进行任何字符串转换。我相信 DateTime 的实际表示纯粹是 UI 的责任。

HTH

丹尼斯

The first example is typically the best way. However, if you would switch o1 and o2, it might work in a single call. Fluent Assertions will try to convert (using Convert.ChangeType) the actual value of a property to the expected value of the property with the same name. In your particular example, it would try to convert the DateTime in Dto1 to a string in Dto2 before comparing the values. But since the string representation of a DateTime is dependent on the culture of the thread, it would not give you predictable results. However, if you would switch o1 and o2, I wouldn't be surprised if Convert.ChangeType would succesfully convert your short datetime back to a DateTIme object.

As a side-note, my DTOs usually just pass the DateTime to the caller without any string conversion. I believe that the actual representation of the DateTime is purely a UI responsibility.

HTH

Dennis

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文