使用Reflection动态覆盖ToString()

biz*_*zah 10 c# reflection overriding tostring

我通常覆盖ToString()方法以输出属性名称和与之关联的值.我有点厌倦了手工编写这些,所以我正在寻找一个动态的解决方案.

主要:

TestingClass tc = new TestingClass()
{
    Prop1 = "blah1",
    Prop2 = "blah2"
};
Console.WriteLine(tc.ToString());
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)

TestingClass:

public class TestingClass
{
    public string Prop1 { get; set; }//properties
    public string Prop2 { get; set; }
    public void Method1(string a) { }//method
    public TestingClass() { }//const
    public override string ToString()
    {
        StringBuilder sb = new StringBuilder();
        foreach (Type type in System.Reflection.Assembly.GetExecutingAssembly().GetTypes())
        {
            foreach (System.Reflection.PropertyInfo property in type.GetProperties())
            {
                sb.Append(property.Name);
                sb.Append(": ");
                sb.Append(this.GetType().GetProperty(property.Name).Name);
                sb.Append(System.Environment.NewLine);
            }
        }
        return sb.ToString();
    }
}
Run Code Online (Sandbox Code Playgroud)

目前输出:

Prop1: System.String Prop1
Prop2: System.String Prop2
Run Code Online (Sandbox Code Playgroud)

期望的输出:

Prop1: blah1
Prop2: blah2
Run Code Online (Sandbox Code Playgroud)

我对其他解决方案持开放态度,它不必使用反射,它只需要产生所需的输出.

Sim*_*ker 18

这对我有用:

public class TestingClass
{
    public string Prop1 { get; set; }//properties
    public string Prop2 { get; set; }
    public void Method1(string a) { }//method
    public TestingClass() { }//const
    public override string ToString()
    {
        StringBuilder sb = new StringBuilder();
        foreach (System.Reflection.PropertyInfo property in this.GetType().GetProperties())
        {
            sb.Append(property.Name);
            sb.Append(": ");
            if (property.GetIndexParameters().Length > 0)
            {
                sb.Append("Indexed Property cannot be used");
            }
            else
            {
                sb.Append(property.GetValue(this, null));
            }

            sb.Append(System.Environment.NewLine);
        }

        return sb.ToString();
    }
}
Run Code Online (Sandbox Code Playgroud)

要使其在任何地方都可用,您可以创建扩展.
无法覆盖扩展中的方法,但它仍然可以简化您的生活.

public static class MyExtensions
{
    public static string ToStringExtension(this object obj)
    {
        StringBuilder sb = new StringBuilder();
        foreach (System.Reflection.PropertyInfo property in obj.GetType().GetProperties())
        {

            sb.Append(property.Name);
            sb.Append(": ");
            if (property.GetIndexParameters().Length > 0)
            {
                sb.Append("Indexed Property cannot be used");
            }
            else
            {
                sb.Append(property.GetValue(obj, null));
            }

            sb.Append(System.Environment.NewLine);
        }

        return sb.ToString();
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以调用ToStringExtension()每个对象.
缺点是,它对列表等不起作用,例如:

var list = new List<string>();
// (filling list ommitted)
list.ToStringExtension();
// output:
// Capacity: 16
// Count: 11
// Item: Indexed Property cannot be used
Run Code Online (Sandbox Code Playgroud)


Ωme*_*Man 5

这是一个扩展,它将报告标准类型,如字符串、整数和日期时间,但也会报告字符串列表(如下所示,AccessPoints上面的答案无法处理)。请注意,输出已对齐,例如:

Name         : Omegaman
ID           : 1
Role         : Admin
AccessPoints : Alpha, Beta, Gamma
WeekDays     : Mon, Tue
StartDate    : 3/18/2014 12:16:07 PM
Run Code Online (Sandbox Code Playgroud)

以下是任何类型的扩展,只要它是一个类。然后它反映了公共和私有属性,如果它们不是 null 则报告它们。

public static string ReportAllProperties<T>(this T instance) where T : class
{

    if (instance == null)
        return string.Empty;

    var strListType = typeof(List<string>);
    var strArrType  = typeof(string[]);

    var arrayTypes   = new[] { strListType, strArrType };
    var handledTypes = new[] { typeof(Int32), typeof(String), typeof(bool), typeof(DateTime), typeof(double), typeof(decimal), strListType, strArrType };

    var validProperties = instance.GetType()
                                  .GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                                  .Where(prop => handledTypes.Contains(prop.PropertyType))
                                  .Where(prop => prop.GetValue(instance, null) != null)
                                  .ToList();

    var format = string.Format("{{0,-{0}}} : {{1}}", validProperties.Max(prp => prp.Name.Length));

    return string.Join(
             Environment.NewLine,
             validProperties.Select(prop => string.Format(format, 
                                                          prop.Name,
                                                          (arrayTypes.Contains(prop.PropertyType) ? string.Join(", ", (IEnumerable<string>)prop.GetValue(instance, null))
                                                                                                  : prop.GetValue(instance, null)))));
}
Run Code Online (Sandbox Code Playgroud)

用法

myInstance.ReportAllProperties()

请注意,这是基于我的博客文章C#:ToString To Report all Properties Even Private Ones Via Reflection,它提供了对正在发生的事情的更可靠的解释。