我有一个带方法的简单类,ToHtmlString()
我想枚举这个对象(引用)的所有私有和实例属性。为什么我不能取值。我看到我需要将对象传递给GetValue
方法,以便运行时知道哪个引用反射将获得哪个值,但似乎我不能那样做?GetValue
枚举此实例的所有属性时如何使用?
public string ToHtmlString()
{
var source = this;
var html = new StringBuilder();
foreach (var property in this.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance))
{
html.AppendLine(
new GenericItemView(
(property.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute)?.Description,
property.GetValue(source).ToString());
}
return html.ToString();
}
看起来你缺少一个结束的“)”和一个 .ToString()
html.AppendLine(new GenericItemView((property.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute)?.Description, property.GetValue(source).ToString()).ToString());
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句