lundi 2 mai 2016

Using reflection on Request crashes w3wp.exe process

Here is my try to make an object dumper:

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));
                }
            }
        }
    }
}

When I am using it to inspect Request (Request.Inspect()) in HomeController's Index method, w3wp.exe process crashes and try-catch block doesn't do it's job.

What is actually happening? Microsoft says, that only unhandled exceptions crash w3wp.exe, but I am calling Request.Inspect() wrapped into parent try-catch block;





Aucun commentaire:

Enregistrer un commentaire