I'm stuck at putting out the values of my objects at the moment. Some of them do have List<string>
properties which causes trouble by using the ToString()
Method. Here is the code I use in my base class to get the name and the value of the properties into a string.
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;
}
Anyhow, the check if the propertie is a List does not work. This might be a dumb question and or a bad way to work with reflection but I'm kinda new to it and will appreciate any help to figure out what is going on.
Aucun commentaire:
Enregistrer un commentaire