2

I use this function to serialize in xml file a collection of object.

    public void SerializeEnvironment()
    {            
            if (xs == null) xs = new XmlSerializer(typeof(IList<Classes.Environment>));

            using (StreamWriter wr = new StreamWriter(ConfigFilePath))
                xs.Serialize(wr, Environments);            
    }

The program works perfectly on my dev machine. But when i make deployment on other computers, the program failed on the serialization method with this error :

System.InvalidCastException: [A]System.Collections.Generic.List1[Product] cannot be cast to [B]System.Collections.Generic.List1[Product]. Type A originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Type B originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterList1.Write3_ArrayOfEnvironment(Object o)

I test many things to resolve but without success. Thanks in advance for any suggestions or resolution :)

In addition, here my object class :

[Serializable]  
public class Environment
{
    public string name { get; set; }
    public string value { get; set; }
    public Environment(){}
    public Environment(string Name, string Value)
    {
        name = Name;
        value = Value;
    }
}

taking a wild guess, but is your dev machine 64 bit? If so, try compiling your project for x86. You can do this by right-clicking your project in Solution Explorer and going to Properties. Click on the Build tab and change the Platform Target option from "Any CPU"/"x64" to "x86". Build your solution and redeploy to try again.

4

3 回答 3

2

大胆猜测,但是您的开发机器是 64 位的吗?如果是这样,请尝试为 x86 编译您的项目。您可以通过在解决方案资源管理器中右键单击您的项目并转到属性来执行此操作。单击 Build 选项卡并将 Platform Target 选项从“Any CPU”/“x64”更改为“x86”。构建您的解决方案并重新部署以重试。

于 2013-02-20T15:58:46.633 回答
1

Try select all you references in solution explorer and set their Copy local property in Properties window to true, so they will be copied to the directory with executable or dll file after compilation. It should help if there are no necessary libraries on the target machine.

于 2013-02-20T16:05:11.607 回答
0

I believe your test machines are not using the same version of .net as your release. If you test machines don't have .net 4.0 install it... If you test machines are running Windows XP and your release is in .net 4.5 you will need to downgrade your program to .net 4.0.

于 2013-02-20T16:01:53.010 回答