Bob Eaton
5/11/2007 8:16:00 AM
I have the following code in my .Net DLL (call it, assemA.dll):
// assume that strProgId is something like: "FooNameSpace.BarClassName"
Type type = Type.GetTypeFromProgID(strProgId);
IFoo rIFoo = (IFoo) Activator.CreateInstance(type);
The IFoo interface is also defined in assemA using an interface statement:
[Guid("42099864-FAAC-SOMEGUID")]
public interface IFoo
{
....
}
And several implementations of IFoo are implemented in assemA.dll as well.
e.g.:
public class CFoo : IFoo
{
...
}
[Aside: the reason for using an interface rather than just the class
implementation directly AND for using Activator.CreateInstance to create it,
rather than just 'new', is because some of the implementations of IFoo are
from MFC/ATL (COM) servers. So sometimes the object created--which gets
casted as an IFoo interface above--is a .Net implementation and sometimes an
external COM implementation]
Since the code that calls CreateInstance above and casts the result as an
IFoo is shared, assemA is installed on users' machines in the global
assembly cache where there is the potential for it to reside "side-by-side"
with different versions of the same assembly. So, C:\Windows\assembly might
have assemA.dll v2.5 and v2.6 at the same time.
Now comes the problem: If one of the client applications was built against a
reference to assemA.dll v 2.5, it will correctly load v2.5, but when I
create one of the implementations of IFoo based on its ProgId, that seems
like it might be a COM interop instantiation rather than a .Net
instantiation. Since COM instantiations will always (I think) go to the
newest implementation, (I think) it will try to get the version of CFoo
implemented in the assemA.dll v 2.6.
The glitch is that even if IFoo does not change (nor does its GUID), since
IFoo is defined in both versions of the same assembly, the v2.5 of the
assembly cannot for some reason cast an implementation of IFoo from the v2.6
assembly using what it knows about IFoo in the v2.5 assembly... I think this
is true, because the call to CreateInstance is throwing an exception that
says, "Unable to cast object of type 'SomeNameSpace.CFoo' to type
'SomeNameSpace.IFoo'.
Does anyone know a) if this situation is accurate as I describe it (i.e. am
I on the right track assuming its a mismatch of some time based on the
different assembly versions), and b) if there's a solution?
Thanks in advance,
Bob