简体   繁体   English

有没有一种简单的方法可以将所有对象的属性获取为字符串?

[英]Is there an easy way of getting all of an objects properties as a string?

I have a class called ReportToDownload that contains several properties. 我有一个名为ReportToDownload的类,其中包含几个属性。 In the Assert part of a test I'm comparing expected and actual lists of ReportToDownload using an equality comparer I have written. 在测试的断言部分中,我正在使用我编写的相等比较器比较ReportToDownload的预期列表和实际列表。 For any items that don't match I was hoping to print out the contents of the expected and actual items as part of the message using: 对于任何不匹配的项目,我希望使用以下方法将预期和实际项目的内容作为消息的一部分打印出来:

actual[i].ToString()

but this just returns the name of the object - ReportToDownload. 但这只是返回对象的名称-ReportToDownload。 Is there any way of easily getting the whole contents of the object as a string? 有什么方法可以轻松地将对象的全部内容作为字符串获取?

Depending on who this is being shown to, a simple "no effort" solution is to use JSON: 根据显示给谁,一个简单的“不费吹灰之力”的解决方案是使用JSON:

Install-Package Newtonsoft.Json

Then... 然后...

JsonConvert.SerializeObject(actual[i])

Rather than using an equality comparison, write a method that compares every individual property using your unit test's framework built-in assertion methods, eg: 而不是使用相等比较,而是使用单元测试的框架内置的断言方法编写一种比较每个单独属性的方法,例如:

public void AssertEquals(ReportToDownload actual, ReportToDownload expected)
{
    Assert.That(actual.PropertyA, Is.EqualTo(expected.PropertyA));
    Assert.That(actual.PropertyB, Is.EqualTo(expected.PropertyB));
}

The built-in assertion methods will print the expected and actual values appropriately when there is a comparison failure. 当比较失败时,内置的断言方法将适当地打印期望值和实际值。

Most likely, your unit test framework will also allow you to write your own assertions that will print the differences in a custom way, if necessary. 最有可能的是,您的单元测试框架还允许您编写自己的断言,必要时将以自定义方式打印差异。

I don't know how useful this could be, but here's my take: 我不知道这可能有多大用处,但这是我的看法:

public class ClassA
{
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }

    public override string ToString()
    {
        string ret = string.Empty;

        foreach (PropertyInfo pi in this.GetType().GetProperties())
        {
            ret += string.Format("{0}: {1}\r\n", pi.Name, pi.GetValue(this, null).ToString());
        }

        return ret;
    }
}

Used like this: 像这样使用:

ClassA a = new ClassA();
a.Prop1 = "Hello";
a.Prop2 = "World";

var toStringOutput = a.ToString();

You may have to handle more complex property types, but for basic ones, that should work. 您可能必须处理更复杂的属性类型,但对于基本属性类型,它应该起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有一种简单的方法可以将对象属性转换为字典<string, string> - Is there an easy way to convert object properties to a dictionary<string, string> 寻找一种快速简便的方法来合并POCO上的所有属性 - Looking for a fast and easy way to coalesce all properties on a POCO 是否有一种简单的方法可以在C#中向所有表单对象添加事件 - Is there an easy way to add an event to all form objects in C# 以C#简单方式重复获取一个字符的字符串 - Getting string with one char repeated in c# easy way 有没有一种简单的方法来检查ICollection中的所有元素 <ISet<string> &gt;一样吗? - Is there an easy way to check if all elements in ICollection<ISet<string>> are identical? 一种简便/简洁的方法来创建新的匿名类型,该类型包含已定义类的所有属性以及一些其他字段? - Easy/Concise way to create new anonymous type that contains all properties of a defined class, plus a few additional fields? 如何将具有int字符串属性的对象列表转换为具有所有字符串属性的对象列表 - How to convert a list of objects with int, string properties to list of objects with all string properties 在对象列表中打印所有属性的最佳方法是什么? - What's the best way to print all properties in a list of objects? 如果类中有许多属性,创建defaultconstructor的简单方法? - Easy way to create defaultconstructor if there are many properties in a class? 枚举类属性然后指定该属性的简便方法 - Easy way to enumerate class properties and then specify that property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM