I have a class that is called regularly by several objects. I would like to include information in any exceptions as who the creater of the object is. What are my options?
"Who" refers to an object or class
Store a stacktrace when the constructor is called. This is similar to what SlimDX does in debug builds.
I might be missing something, but I am pretty sure that the only way you can do it, is by manually passing this information, fe. in constructor of your object.
Edit : If this is what you were looking for? :
class Creator
{
    public string Name { get; private set; }
    public Creator(string name)
    {
        Name = name;
    }
}
class Foo
{
    readonly Creator creator;
    public Foo(Creator creator)
    {
        this.creator = creator;
    }
    public void DoSth()
    {
        throw new Exception("Unhandled exception. My creator is " + creator.Name);
    }
}
public static void Main()
{
    Foo f = new Foo(new Creator("c1"));
    f.DoSth();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With