PropertyDescriptor.SetValue Exception call stack

188 views Asked by At

I have some code that uses a PropertyDescriptor to set a value. The problem is that when it fails, the exception does not contain information or a callstack that includes the executed code that threw the exception? In this case SetNameInternal(). The exception only has information up to the point SetValue was invoked. There is no inner exception. Is there any way to get this information so it can be logged? (such as when running the application outside of a debugger)

The following code demonstrates the issue. The string returned by Exception.ToString() only contains :

System.Exception: Ah! at System.ComponentModel.ReflectPropertyDescriptor.SetValue(Object component, Object value) at ConsoleApp54.Program.Main(String[] args) ... ConsoleApp54.exe' has exited with code 0 (0x0).

Yet the problem is in SetNameInternal(). Of course in this simple example, it's obvious what is wrong, but a real world case will have a lot more going on, so the callstack is useful.

    public class Test
{
    public string Name
    {
        get { return m_name; }
        set
        {
            m_name = value;
            SetNameInternal();
        }
    }

    void SetNameInternal()
    {
        throw new Exception("Ah!");
    }

    private string m_name;
}

class Program
{
    static void Main(string[] args)
    {
        var t = new Test();
        try
        {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(t);
            var desc = properties.Find("Name", false);
            desc.SetValue(t, "Fail");
            //but this would return different StackTrace
            //t.Name = "Fail";
        }
        catch (Exception e)
        {
            Debug.Write(e.ToString());
        }
    }
}

fiddle

0

There are 0 answers