I'm getting a strange InvalidCastException when I try to convert the object
I get from an AppDomain.CreateInstanceAndUnwrap call. What I'm trying to do
is create a proxy object in my own AppDomain so that I can define my own
AppBase and config file.
Now, the assembly is loaded, and I can tell because of the constructor
outputs in the debug console, and I can get an "object" out of the
CreateAndUnwrap. But, if I try to cast it either to the implemented
interface or even just the object itself, I get an InvalidCastException.
Any ideas?
Thanks,
--Oren
class TraceabilityAddIn : MarshalByRefObject, ITraceabilityAddIn
{
...
}
interface ITraceabilityAddIn { ... }
class TraceabilityAddInProxy : ITraceabilityAddIn
{
ITraceabilityAddIn _AddIn;
void Init() {
// Get the path we're executing in
string path = new
System.IO.FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName;
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = path;
setup.ApplicationName = "Traceability AddIn";
setup.ConfigurationFile = "Traceability.config";
AppDomain ad = AppDomain.CreateDomain("TraceabilityDomain" , null, setup);
try
{
_AddIn =
(ITraceabilityAddIn)ad.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly
().FullName, typeof(TraceabilityAddIn).FullName);
// It doesn't matter whether I hard-code the assembly and type name or not
//_AddIn =
(ITraceabilityAddIn)ad.CreateInstanceAndUnwrap("Traceability.AddIn",
"Traceability.AddIn.TraceabilityAddIn");
}
catch(Exception e)
{
// I get an exception--I shouldn't
Debug.WriteLine(e.Message);
Debug.WriteLine(e.StackTrace);
}
}
}
You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced
DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.