473,396 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

XmlSerializer fails on object loaded from an activated assembly

I'm loading a plugin assembly using Activator.CreateInstanceFrom, and
inside this assembly is a settings class which gets serialized to XML.

The general code flow is as follows:

ObjectHandle pluginHandle = Activator.CreateInstanceFrom(assemblyName,
type);
object plugin = pluginHandle.Unwrap();
....

// Settings is from the plugin Assembly loaded by the Activator
XmlSerializer x = new XmlSerializer(typeof(Settings));
x.Serialize(settingsFile, _settings);
// blows up here with this exception:

System.InvalidOperationException: There was an error generating the
XML document. ---System.InvalidCastException: Unable to cast object
of type '*.Settings' to type '*.Settings'.
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationWriterSettings.Write3_Settings(Object
o)
Which is confusing considering the types are identical. However, when
I add the following XML to App.Config, the error goes away:

<system.diagnostics>
<switches>
<add name="XmlSerialization.Compilation" value="4" />
</switches>
</system.diagnostics>

What am I missing w.r.t. Activator.CreateInstanceFrom and
XmlSerializer?

Thanks.

Apr 9 '07 #1
2 5062
On Apr 9, 10:53 am, christopher.watf...@gmail.com wrote:
I'm loading a plugin assembly using Activator.CreateInstanceFrom, and
inside this assembly is a settings class which gets serialized to XML.

The general code flow is as follows:

ObjectHandle pluginHandle = Activator.CreateInstanceFrom(assemblyName,
type);
object plugin = pluginHandle.Unwrap();
...

// Settings is from the plugin Assembly loaded by the Activator
XmlSerializer x = new XmlSerializer(typeof(Settings));
x.Serialize(settingsFile, _settings);
// blows up here with this exception:

System.InvalidOperationException: There was an error generating the
XML document. ---System.InvalidCastException: Unable to cast object
of type '*.Settings' to type '*.Settings'.
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationWriterSettings.Write3_Settings(Object
o)

Which is confusing considering the types are identical. However, when
I add the following XML to App.Config, the error goes away:

<system.diagnostics>
<switches>
<add name="XmlSerialization.Compilation" value="4" />
</switches>
</system.diagnostics>

What am I missing w.r.t. Activator.CreateInstanceFrom and
XmlSerializer?

Thanks.
Moreover, *any* type from a loaded assembly (Assembly.Load,
Assembly.LoadFile, Assembly.LoadFrom, Activator.CreateInstanceFrom)
fails to be serialized/deserialized.

Assembly asm = Assembly.LoadFrom(Path.GetFullPath(assemblyName));
object plugin = asm.CreateInstance(type);
((IPlugin)plugin).WriteSettings(textWriter);

....

public class XmlTest { public string Hello = "Hello World!"; }

public void WriteSettings(TextWriter settingsWriter)
{
XmlSerializer x = new XmlSerializer(typeof(XmlTest));
x.Serialize(settingsWriter, new XmlTest());
}
Once again, if I use the diagnostic switches, everything works fine.

Apr 9 '07 #2
On Apr 9, 11:26 am, christopher.watf...@gmail.com wrote:
On Apr 9, 10:53 am, christopher.watf...@gmail.com wrote:
I'm loading a plugin assembly using Activator.CreateInstanceFrom, and
inside this assembly is a settings class which gets serialized to XML.
The general code flow is as follows:
ObjectHandle pluginHandle = Activator.CreateInstanceFrom(assemblyName,
type);
object plugin = pluginHandle.Unwrap();
...
// Settings is from the plugin Assembly loaded by the Activator
XmlSerializer x = new XmlSerializer(typeof(Settings));
x.Serialize(settingsFile, _settings);
// blows up here with this exception:
System.InvalidOperationException: There was an error generating the
XML document. ---System.InvalidCastException: Unable to cast object
of type '*.Settings' to type '*.Settings'.
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationWriterSettings.Write3_Settings(Object
o)
Which is confusing considering the types are identical. However, when
I add the following XML to App.Config, the error goes away:
<system.diagnostics>
<switches>
<add name="XmlSerialization.Compilation" value="4" />
</switches>
</system.diagnostics>
What am I missing w.r.t. Activator.CreateInstanceFrom and
XmlSerializer?
Thanks.

Moreover, *any* type from a loaded assembly (Assembly.Load,
Assembly.LoadFile, Assembly.LoadFrom, Activator.CreateInstanceFrom)
fails to be serialized/deserialized.

Assembly asm = Assembly.LoadFrom(Path.GetFullPath(assemblyName));
object plugin = asm.CreateInstance(type);
((IPlugin)plugin).WriteSettings(textWriter);

...

public class XmlTest { public string Hello = "Hello World!"; }

public void WriteSettings(TextWriter settingsWriter)
{
XmlSerializer x = new XmlSerializer(typeof(XmlTest));
x.Serialize(settingsWriter, new XmlTest());

}

Once again, if I use the diagnostic switches, everything works fine.
I have found a solution which, while not perfect, works. The assembly
seemingly cannot be loaded from outside the ApplicationBase or
CodeBase or PrivateBinPath. Moving the plugin DLL's to a path
underneath the working directory solved things.

DOES NOT WORK:
\MyExecutable\bin\Debug\MyExecutable.exe
\MyPlugin\bin\Debug\MyPlugin.dll

WORKS:
\MyExecutable\bin\Debug\MyExecutable.exe
\MyExecutable\bin\Debug\Plugins\MyPlugin.dll

However, I'm not entirely sure why the XmlSerializer fails to see
Assemblies loaded from elsewhere. Especially when the diagnostic
switches make it work!

Apr 9 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Johan Appelgren | last post by:
Hi, I'm trying to serialize a class hierarchy using System.Xml.Serialization.XmlSerializer from a class that is loaded as a Com object. But it fails with a InvalidCastException. I don't know...
5
by: aladdinm1 | last post by:
Hi All, I have an annoying trouble with binary serialization. I have a windows forms application which works like a server and keeps sending data to its clients. The data is serialized before...
2
by: Chris Aitchison | last post by:
Hello, I am attempting to have a class that I have written serialize so that it can be both passed as a parameter or return value for a webservice, and also be serialized to disk using the...
0
by: Magnus Lindhe | last post by:
Hello, I'm trying to deserialize an XML document into a objects with the XmlSerializer which works fine in my NUnit test suit but fails with a TypeLoadException in the server. The difference...
5
by: Keith Bannister | last post by:
I'm new to .net so here goes. I'm tying to deserialize a class that is associated with an XML schema. I created the C# class with xsd.exe as below: xsd.exe /c /n:somenamespace...
12
by: SJD | last post by:
I've just read Christoph Schittko's article on XmlSerializer: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnxmlnet/html/trblshtxsd.asp . . . and very informative it is too....
4
by: BuddyWork | last post by:
Hello, When running the following line of code XmlSerializer serializer = new XmlSerializer(typeof (MyXmlTestClass)); I get the following exception message. An unhandled exception of
0
by: Philipp Sumi | last post by:
Hi all I have a strange situation with a VS.net Add-In that uses the XmlSerializer class for deserialization. In the code below, I pass a given type to the serializer class. However, the type of...
8
by: cd~ | last post by:
I can provide a test app, the news server won't allow me to post the files because they are too large (93KB and 1.2KB) I downloaded the ESRI ArcXml schema and generated the classes from the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.