2
votes

I have an exe that does data dumps. The exe will dynamically pick up DLL's based on configuration and pass a class object into it. The DLL has a copy of this class compiled with it and can see the data, under debug, without a problem as an object. However, when I try to cast that to the class, it tells me it can't because of the context. I'm sure I've overlooked something as I do that at times.

Error:

[A]MyClass cannot be cast to [B]MyClass. Type A originates from 'MyExe, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'C:\MyPath\MyExe.exe'. Type B originates from 'MyDLL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' at location 'C:\MyPath\MyDLL.dll'.

EXE Code:

Object[] param = new Object[] { MyClass };
MethodInfo m = type.GetMethod("MyMethod");
reader = (SqlDataReader)m.Invoke(obj, param);

DLL Code:

public SqlDataReader MyMethod(Object param)
{
    SqlDataReader reader = new SqlDataReader();
    Type t = param.GetType();  //Returns MyClass

    if (param is MyClass)      //Returns false
        reportItem = (MyClass)param; //Never executes

    MyClass reportItem = (MyClass)param; //InvalidCastException

    //other code here, pulling data
    return reader;
}
1
My guess is you have to dynamically create the class object that you pass in as a parameter. OR inside MyMethod, create a new MyClass using the properties on the passed in object. - Mike C.

1 Answers

3
votes

The DLL has a copy of this class compiled with it

Don't do that, basically. You should have the type in one assembly, and only one assembly. As far as the CLR is concerned, these are entirely different types.

You probably want to have a common library which both the plugins and your application can refer to. Or you could make your plugins refer to the application executable and keep the type within there.