views:

228

answers:

1

hi,

I have this weird problem that I cannot handle myself. A class in the model of my mvp-project designed as singleton causes an InvalidCastException. The source of error is found in this code line where the deserialised object is assigned to the instance variable of the class: engineObject = (ENGINE)xSerializer.Deserialize(str); It occurs whenever I try to add one of my UserControls to a Form or to a different UC. All of my UCs have a special presenter that access the above mentioned instance variable of the singleton class. This is what I get when trying to add a UC somewhere:

'System.TypeInitializationException: The type initializer for 'MVP.Model.EngineData' threw an exception. ----> System.InvalidCastException: [A]Engine cannot be cast to [B]Engine. Type A originates from 'MVP.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' at location '[...]\AppData\Roaming\Microsoft\VisualStudio\9.0\ProjectAssemblies\uankw1hh01\MVP.Model.dll'. Type B originates from 'MVP.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' at location '[...]\AppData\Roaming\Microsoft\VisualStudio\9.0\ProjectAssemblies\u_hge2de01\MVP.Model.dll'...

So I somehow have two assemblies and they are not accessed from my project folder, but from a VS temp folder? I googled a lot and only found this: IronPython Exception: [A]Person cannot be cast to [B]Person. There is a solution offered, but first it concerns IronPhyton and second I don't know where to use it within my project?

It would be just great, if u could help me out here :-) thx

+4  A: 

Types are per-assembly; if you have "the same" assembly loaded twice, then types in each "copy" of the assembly are not considered to be the same type.

These issues normally crop up when the two assemblies are in the Load and LoadFrom contexts. See

http://stackoverflow.com/questions/1477843/difference-between-loadfile-and-loadfrom-with-net-assemblies

and the link to suzcook's blog for details on that issue.

Also, consider using the fusion log viewer to help diagnose the problem.

http://msdn.microsoft.com/en-us/library/e74a18c4%28VS.71%29.aspx

Eric Lippert
hey eric, first of all: thx !!! you are the first to give me any further hints and help on this case. I have rushed through the articles (and will read them again), but I've already got a question: I am not loading any assemblies by myself in the code. I am just referencing projects and then add a UC with the help of the designer to a different UC or Form. So my question is, where should I start looking for such load failures as the IDE takes care of all loading processes?
LLEA
I've tried to use the fusion log viewer, but could not see anything (most likely because I did not make proper use of it).Besides that, again the question how helpfull is this tool as my failures take place before runtime?
LLEA