Is there an easy way to get the object that created this object. I know that it will always be of some object type, but I was hoping for some easy keyword to use like this while in the constructor.
Possibly Unnecessary Context:
I have been creating unit tests for this add-in I am working on in PowerPoint. Sometimes you need to go one level up and look at the parent object to get information such as position, font size, etc. I tried to avoid this up until now.
I suppose I can edit my "Mocks" and add another constructor where I pass the object that creates each object to its child; however, I feel like there is a better solution out there. Oh also, these mocks are partially implementing interfaces provided by Visual Studios Tools for Office and part of their interface is the Parent property getter/setter. If I don’t implement a method it simply throws a new NotImplementedException when that method is called.
You can get the calling method from the stack, and deduce the class name from that:
http://www.csharp-examples.net/reflection-calling-method-name/
However, this is fragile in the sense that, if you ever have a layer inbetween, the position of the caller you care about in the stack would shift. If you place the logic in a utility method or extension method, though, it would not be hard to evolve over time.
Querying the call stack is not a terribly fast operation, but for unit tests, it would probably be acceptable.