在 C# 中使用反射检查 属性 是否为列表
Check if Property is List using Reflection in C#
我目前无法给出对象的值。其中一些确实具有 List<string>
属性,这会导致使用 ToString()
方法时出现问题。这是我在基础 class 中使用的代码,用于将属性的名称和值转换为字符串。
public override string ToString()
{
string content = "";
foreach (var prop in this.GetType().GetProperties())
{
if (prop.PropertyType is IList<string> && prop.GetType().IsGenericType && prop.GetType().GetGenericTypeDefinition().IsAssignableFrom(typeof(List<>)))
content += prop.Name + " = " + PrintList((List<string>)prop.GetValue(this));
else
content += prop.Name + " = " + prop.GetValue(this) + "\r\n";
}
content += "\r\n";
return content;
}
private string PrintList(List<string> list)
{
string content = "[";
int i = 0;
foreach (string element in list)
{
content += element;
if (i == list.Count)
content += "]";
else
content += ", ";
}
return content;
}
无论如何,检查属性是否为列表不起作用。这可能是一个愚蠢的问题,或者是处理反射的糟糕方法,但我对它有点陌生,如果能帮助我弄清楚发生了什么,我将不胜感激。
我会这样做;
var property = prop.GetValue(this);
// try to cast as IEnumerable<string> -- will set to null if it's not.
var propertyStrings = property as IEnumerable<string>;
if (propertyStrings != null) {
foreach(var s in propertyStrings) {
// do something here with your strings.
}
}
此外,不要使用 +
运算符连接字符串,请查看 StringBuilder
,这更利于内存和速度。
public override string ToString()
{
StringBuilder content = new StringBuilder();
foreach (var prop in this.GetType().GetProperties())
{
var propertyType = prop.PropertyType;
var propertyValue = prop.GetValue(this);
if (propertyValue != null)
{
if (propertyValue is IEnumerable<string>)
content.AppendFormat("{0} = {1}", prop.Name, PrintList(propertyValue as IEnumerable<string>));
else
content.AppendFormat("{0} = {1}", prop.Name, propertyValue.ToString());
}
else
content.AppendFormat("{0} = null", prop.Name);
content.AppendLine();
}
return content.ToString();
}
private string PrintList(IEnumerable<string> list)
{
var content = string.Join(",", list.Select(i => string.Format("[{0}]", i)));
return content;
}
我目前无法给出对象的值。其中一些确实具有 List<string>
属性,这会导致使用 ToString()
方法时出现问题。这是我在基础 class 中使用的代码,用于将属性的名称和值转换为字符串。
public override string ToString()
{
string content = "";
foreach (var prop in this.GetType().GetProperties())
{
if (prop.PropertyType is IList<string> && prop.GetType().IsGenericType && prop.GetType().GetGenericTypeDefinition().IsAssignableFrom(typeof(List<>)))
content += prop.Name + " = " + PrintList((List<string>)prop.GetValue(this));
else
content += prop.Name + " = " + prop.GetValue(this) + "\r\n";
}
content += "\r\n";
return content;
}
private string PrintList(List<string> list)
{
string content = "[";
int i = 0;
foreach (string element in list)
{
content += element;
if (i == list.Count)
content += "]";
else
content += ", ";
}
return content;
}
无论如何,检查属性是否为列表不起作用。这可能是一个愚蠢的问题,或者是处理反射的糟糕方法,但我对它有点陌生,如果能帮助我弄清楚发生了什么,我将不胜感激。
我会这样做;
var property = prop.GetValue(this);
// try to cast as IEnumerable<string> -- will set to null if it's not.
var propertyStrings = property as IEnumerable<string>;
if (propertyStrings != null) {
foreach(var s in propertyStrings) {
// do something here with your strings.
}
}
此外,不要使用 +
运算符连接字符串,请查看 StringBuilder
,这更利于内存和速度。
public override string ToString()
{
StringBuilder content = new StringBuilder();
foreach (var prop in this.GetType().GetProperties())
{
var propertyType = prop.PropertyType;
var propertyValue = prop.GetValue(this);
if (propertyValue != null)
{
if (propertyValue is IEnumerable<string>)
content.AppendFormat("{0} = {1}", prop.Name, PrintList(propertyValue as IEnumerable<string>));
else
content.AppendFormat("{0} = {1}", prop.Name, propertyValue.ToString());
}
else
content.AppendFormat("{0} = null", prop.Name);
content.AppendLine();
}
return content.ToString();
}
private string PrintList(IEnumerable<string> list)
{
var content = string.Join(",", list.Select(i => string.Format("[{0}]", i)));
return content;
}