Help: specified cast is not valid.

  • Thread starter Thread starter Bin Chen
  • Start date Start date
B

Bin Chen

All,

I am using XmlSerializer in a VS.NET 2003 add-in project to save data.
I have noticed sometimes all XmlSerialization fail, the error message
is "specified cast is not valid.".

Philipp Sumi had the same issue as I do ( and there are more details ):
http://groups.google.com/group/micr...zer+projectassemblies&rnum=1#8709c92ab5bc92fb
( watch for linebreaks )

The real cause of the problem appears to be two same assemblies were
loaded to devenv.exe application domain. One from the APPBASE directory
( loaded by Assembly.LoadFrom()? ), the other from VS.NET
ProjectAssemblies directory ( loaded by Assembly.Load()? ). The types
in these two loaded assemblies can't be casted to each other even
though they are from _in_fact_ the same assemblies.

Unfortunately, the two solutions provided in Philipp Sumi's posts
donnot really work for me ( I think ):
1.Make assembly strong name and install to GAC:
My assembly depends on other files/assemblies, and it is still under
active development. I cannot ask all developers in the team to
reinstall the assembly everytime a new version is out. I think
automatically install assembly to GAC for every new build is
overkilled.

2.Custom serialization seems a lot of work.


There are still a few questions which I has failed to find any answers:

1. What triggers the shadow copy of VS.NET, and _why_?
I have noticed these scenarios but I donot know why:
a. If I launch the add-in right after a successful rebuild to the
same add-in project, the XmlSerializer.Deserialize() call triggers
shadow copy.
b. If there are opened forms in the solution, it may triggers
shadow copy when the solution loaded.

2. Why XmlSerializer.Deserialize() (through Reflection) triggers
shadow copy?

3. From JunFengZhang's blog:
http://blogs.msdn.com/junfeng/archive/2005/05/31/423340.aspx, I have
the impression that Add-In uses Assembly.LoadFrom() to load Add-In
assemblies,
Am I correct if I claim:
a. all assmblies loaded directly for Add-In are loaded by
Assembly.LoadFrom()?
b. when Add-In is doing the XmlSerialization, Assmbly.Load() was
used to loaded the shadow copied assembly.

4. How do I know whether the given assembly loaded by Assembly.Load()
or Assembly.LoadFrom()?

5. If I have two assemblies loaded in current application domain, one
through Load(), the other through LoadFrom(). Is it possible to convert
the types between the assemblies, I meant if I have an instance of one
type from one assmbliy, can I use it to construct/populate the same
type from the other assembly?
 
Back
Top