【发布时间】:2016-05-02 16:47:12
【问题描述】:
这是我制作对象转储器的尝试:
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
namespace Inspector
{
public static class Inspector
{
public static string Inspect(this object o)
{
StringBuilder sb = new StringBuilder();
InspectObject(o, sb);
return sb.ToString();
}
private static void InspectObject(object o, StringBuilder sb)
{
Type objType = o.GetType();
if (objType == typeof(string))
sb.Append(o.ToString());
else
{
try
{
IList<PropertyInfo> props = new List<PropertyInfo>(objType.GetProperties());
foreach (PropertyInfo prop in props)
{
object propValue = null;
ParameterInfo[] ip = prop.GetIndexParameters();
if (ip.Length == 0)
{
propValue = prop.GetValue(o, null);
InspectObject(propValue, sb);
}
else
{
}
}
}
catch (Exception ex)
{
sb.Append(string.Format("Exception: {0}", ex.Message));
}
}
}
}
}
当我在 HomeController 的 Index 方法中使用它检查 Request (Request.Inspect()) 时,w3wp.exe 进程崩溃并且 try-catch 块不起作用。
实际发生了什么?微软说,只有未处理的异常会导致 w3wp.exe 崩溃,但我调用的是 Request.Inspect() 包裹在父 try-catch 块中;
【问题讨论】:
-
请注意,StackOverflow(这是递归的常见结果)会杀死进程并且无法被捕获...
标签: c# reflection crash w3wp