I have a .net assembly that exposes a public class (named A) to be created other programs using reflection (Assembly.CreateInstance). For now, it works fine.
But now I have two subclasses of A, say A1 and A2, and I need to create one or the other, depending on a runtime check. The client cannot be modified, it should believe that the object returned is of class A.
If I were using COM instead of .net, it would be easy enough: simply add the check into the IClassFactory.CreateInstance and be done. But is there a way to do this in .net?
Note: the obvious solution of using a proxy object, or similar, is not practical, because class A has several hundred methods (!) and I don’t want to reroute all of them.
Firstly, it should be noted that it would be easier if your client could use a factory method that you provide rather than having to hijack
Activator.CreateInstance. Please consider that fully before reading on…Here’s an example using
ContextBoundObject– it uses a counter as the “runtime check” to decide betweenBandCas the actual object, even though the caller asked for anA. Note that this also works if they usednew A().So yes, you can be dastardly and lie to the caller by giving them back something other than they asked for ;p This is fairly evil code, though. YMMV etc.