使用反射的ToString动态倍率() [英] Dynamic override of ToString() using Reflection
问题描述
我一般重写ToString()方法输出属性的名称和与之关联的值。 。我有点累了手工写这些,所以我正在寻找一个动态的解决方案的
主营:
TestingClass TC =新TestingClass()
{
为prop1 =blah1,
Prop2 =blah2
};
Console.WriteLine(tc.ToString());
到Console.ReadLine();
TestingClass:
公共类TestingClass
{
公共字符串为prop1 {搞定;组; } //特性
公共字符串Prop2 {搞定;组; }
公共无效方法1(字符串){} //方法
公共TestingClass(){} //常量
公共重写字符串的ToString()
{
的StringBuilder SB =新的StringBuilder();
的foreach(在System.Reflection.Assembly.GetExecutingAssembly类型类型()。GetTypes())
{
的foreach(在type.GetProperties System.Reflection.PropertyInfo财产())
{
sb.Append(property.Name);
sb.Append(:);
sb.Append(this.GetType()的getProperty(property.Name).Name点);
sb.Append(System.Environment.NewLine);
}
}
返回sb.ToString();
}
}
这目前输出:
$ B $ System.String为prop1Prop2:System.String Prop2
为prop1 >
所需的输出:
为prop1:blah1
Prop2:blah2
我打开其他的解决方案,它不必须使用反射,它只是具有产生所需的输出。
解决方案 这对我的作品:
公共类TestingClass
{
公共字符串为prop1 {搞定;组; } //特性
公共字符串Prop2 {搞定;组; }
公共无效方法1(字符串){} //方法
公共TestingClass(){} //常量
公共重写字符串的ToString()
{
的StringBuilder SB =新的StringBuilder();
的foreach(在this.GetType System.Reflection.PropertyInfo财产()的GetProperties())
{
sb.Append(property.Name);
sb.Append(:);
如果(property.GetIndexParameters()长度方式> 0)
{
sb.Append(索引属性不能用);
}
,否则
{
sb.Append(property.GetValue(这一点,空));
}
sb.Append(System.Environment.NewLine);
}
返回sb.ToString();
}
}
为了让随处可见,你可以创建一个扩展。结果
这不是可以覆盖在扩展方法,但还是应该简化你的生活。
公共静态类MyExtensions
{
公共静态字符串ToStringExtension(此对象OBJ)
{
StringBuilder的SB =新的StringBuilder();
的foreach(在obj.GetType System.Reflection.PropertyInfo财产()的GetProperties())
{
sb.Append(property.Name);
sb.Append(:);
如果(property.GetIndexParameters()长度方式> 0)
{
sb.Append(索引属性不能用);
}
,否则
{
sb.Append(property.GetValue(OBJ,NULL));
}
sb.Append(System.Environment.NewLine);
}
返回sb.ToString();
}
}
然后可以调用 ToStringExtension ()
中的每个对象结果
不足之处是,它不完全适用于名单等工作的例如:
VAR名单=新名单,LT;串>();
//(填充列表ommitted)
list.ToStringExtension();
//输出:
//容量:16
//次数:11
//项目:索引属性不能用
I generally override the ToString() method to output the property names and the values associated to them. I got a bit tired of writing these by hand so I'm looking for a dynamic solution.
Main:
TestingClass tc = new TestingClass()
{
Prop1 = "blah1",
Prop2 = "blah2"
};
Console.WriteLine(tc.ToString());
Console.ReadLine();
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();
}
}
This currently outputs:
Prop1: System.String Prop1
Prop2: System.String Prop2
Desired Output:
Prop1: blah1
Prop2: blah2
I'm open for other solutions, it doesn't have to use reflection, it just has to produce the desired output.
解决方案 This works for me:
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();
}
}
To make it available everywhere you can create an Extension.
It's not possible to override methods in an Extension, but still it should simplify your life.
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();
}
}
You can then call ToStringExtension()
on every object.
Downside is, it doesn't work perfectly for lists etc., example:
var list = new List<string>();
// (filling list ommitted)
list.ToStringExtension();
// output:
// Capacity: 16
// Count: 11
// Item: Indexed Property cannot be used
这篇关于使用反射的ToString动态倍率()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!