C# 使用C中的反射检查属性是否为List#

C# 使用C中的反射检查属性是否为List#,c#,list,reflection,tostring,C#,List,Reflection,Tostring,我现在被困在展示我的物品的价值上。其中一些确实具有List属性,使用ToString()方法会导致问题。下面是我在基类中用于将属性的名称和值转换为字符串的代码 public override string ToString() { string content = ""; foreach (var prop in this.GetType().GetProperties()) { if (prop.PropertyT

我现在被困在展示我的物品的价值上。其中一些确实具有
List
属性,使用
ToString()
方法会导致问题。下面是我在基类中用于将属性的名称和值转换为字符串的代码

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;
    }
public重写字符串ToString()
{
字符串内容=”;
foreach(此.GetType().GetProperties()中的var prop)
{
如果(prop.PropertyType为IList&&prop.GetType().IsGenericType&&prop.GetType().GetGenericTypeDefinition().IsAssignableFrom(typeof(List)))
content+=prop.Name+“=”+PrintList((List)prop.GetValue(this));
其他的
content+=prop.Name+“=”+prop.GetValue(this)+“\r\n”;
}
内容+=“\r\n”;
返回内容;
}
私有字符串打印列表(列表)
{
字符串内容=“[”;
int i=0;
foreach(列表中的字符串元素)
{
内容+=元素;
if(i==list.Count)
内容+=“]”;
其他的
内容+=“,”;
}
返回内容;
}
无论如何,检查属性是否为列表不起作用。这可能是一个愚蠢的问题,或者是一种不好的思考方式,但我对它有点陌生,希望能有人帮助我弄清楚到底发生了什么

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.    
    }   
}
var property=prop.GetValue(此);
//尝试强制转换为IEnumerable——如果不是,则将设置为null。
var propertyStrings=作为IEnumerable的属性;
if(propertyStrings!=null){
foreach(PropertyString中的var s){
//用你的弦做点什么。
}   
}
另外,不要用
+
操作符连接字符串,而是看一下
StringBuilder
,这对内存和速度都有好处

public重写字符串ToString()
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;
}
{ StringBuilder内容=新建StringBuilder(); foreach(此.GetType().GetProperties()中的var prop) { var propertyType=prop.propertyType; var propertyValue=prop.GetValue(此); if(propertyValue!=null) { if(propertyValue是IEnumerable) AppendFormat(“{0}={1}”,prop.Name,PrintList(propertyValue为IEnumerable)); 其他的 AppendFormat(“{0}={1}”,prop.Name,propertyValue.ToString()); } 其他的 AppendFormat(“{0}=null”,prop.Name); content.AppendLine(); } 返回content.ToString(); } 私有字符串打印列表(IEnumerable列表) { var content=string.Join(“,”,list.Select(i=>string.Format(“[{0}]”,i)); 返回内容; }
try是IEnumerable
prop.PropertyType.IsAssignableFrom(typeof(IList))
我看到在
字符串上使用
+=
效率低下,但为什么
+
会不好呢?@Downvoter与+=。要执行a=b+c,需要在内存中分配字符串
b+c
,然后分配给a。比如说,添加1M字符串需要1M字符串分配,每次分配都会随着结果越来越大而变得越来越昂贵。如果所有字符串的长度都是
s
chars,那么加起来就是O(s+2s+3s+4s+5s+6s…1000000秒)。我想那是O(s.n^2)。StringBuilder预先分配内存,当数组用完时将其加倍,因此添加一个字符串是O(s),所有字符串都是O(s.n)。感谢您的
StringBuilder
提示@史蒂夫库珀的作品完美!谢谢