Код: Выделить всё
static public void PrintProperties(object obj, int indent)
{
if (obj == null) return;
string indentString = new string(' ', indent);
Type objType = obj.GetType();
PropertyInfo[] properties = objType.GetProperties();
foreach (PropertyInfo property in properties)
{
object propValue = property.GetValue(obj, null);
if (property.PropertyType.Assembly == objType.Assembly && !property.PropertyType.IsEnum)
{
Console.WriteLine("{0}{1}:", indentString, property.Name);
PrintProperties(propValue, indent + 2);
}
else
{
if (null != propValue)
{
Type t = propValue.GetType();
//Console.WriteLine(":::::{0}:::::", propValue.GetType());
bool isDict = t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Dictionary);
if (isDict)
{
Type keyType = t.GetGenericArguments()[0];
Type valueType = t.GetGenericArguments()[1];
foreach (KeyValuePair kvp in (Dictionary)propValue)
{
Console.WriteLine(string.Format("Key = {0}, Value = {1}", kvp.Key, kvp.Value));
}
}
}
Console.WriteLine("{0}{1}: {2}", indentString, property.Name, propValue);
}
}
}
Проблема в том, что я извлекаю тип ключа и значение с помощью:
Код: Выделить всё
Type keyType = t.GetGenericArguments()[0];
Type valueType = t.GetGenericArguments()[1];
Код: Выделить всё
foreach (KeyValuePair kvp in (Dictionary)propValue)
Что мне не хватает?
Спасибо.
PS: .net 4.5.1
Подробнее здесь: https://stackoverflow.com/questions/403 ... reflection