현재 내 개체의 값을 내고 있습니다. 그들 중 일부는 ToString()
메서드를 사용하여 문제를 일으키는 List<string>
속성이 있습니다. 다음은 기본 클래스에서 속성의 이름과 값을 문자열로 가져 오는 데 사용하는 코드입니다.속성이 C#의 리플렉션을 사용하여 목록인지 확인합니다.
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;
}
아무튼, 속성이 목록인지 확인하지 않습니다. 이것은 멍청한 질문 일 수도 있고 반성과 함께 일하는 나쁜 방법 일 수도 있지만, 나는 그것에 익숙하지 않고 어떤 일이 일어나고 있는지를 이해하는 데 도움이 될 것입니다.
시도인가를 IEnumerable – Steve
'prop.PropertyType.IsAssignableFrom (대해서 typeof (IList의))' –
Nkosi