It seems that going through the callstack is the only way to determine which function may have called the function currently being executed. Is this thread-safe, or are there other alternatives (I'm pretty new to reflection; can it do that)?
Here's a bit of the design that leads me to ask this question. I'd be interested to know if there is a better way to accomplish this.
The idea behind what I'm doing is to build a factory class, which will produce the derived forms of a certain base class. It looks something like this:
Expand|Select|Wrap|Line Numbers
- static public class MyFactory<MyClassT> where MyClassT : MyClass, new()
- {
- static public MyClassT CreateMyClassT()
- {
- MyClassT retClass = new MyClassT();
- // ... details here
- return retClass;
- }
- }
Expand|Select|Wrap|Line Numbers
- [assembly: InternalsVisibleTo("MyFactory.cs")]
- public class MyClass_Derived : MyClass
- {
- internal MyClass_Derived()
- : base(MyClassEnum.DerivedType)
- {
- }
- }
- }
That leaves me with the following:
- In order for the factory to create a new MyClassT, I need the : new() restriction.
- With the : new() restriction, the class must have a public constructor with no arguments.
- I wanted to get rid of the public argument-free constructor so that only the factory can construct these objects.
It appears that there is no way to have a generic Factory for a hierarchy of classes that doesn't have a public constructor. So, I thought, if I could check the class that called the constructor and throw an Exception if something other than the Factory tries to call it, I might approximate what I want.
Is there a better way to do this?
Thank you!
--
okay, so somehow I missed the .NET forum and ended up in C/C++. Sorry about that! (How do I move/delete this thread?)