c# - XmlSerializer 在序列化动态加载的类型时抛出异常

标签 c# reflection serialization

我正在尝试使用 System.Xml.Serialization.XmlSerializer 来序列化动态加载(和编译的类)。如果我将有问题的类构建到主程序集中,一切都会按预期进行。但是,如果我从动态加载的程序集中编译和加载该类,XmlSerializer 会引发异常。

我做错了什么?

我创建了以下 .NET 3.5 C# 应用程序来重现该问题:

using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Text;
using System.Reflection;
using System.CodeDom.Compiler;
using Microsoft.CSharp;

public class StaticallyBuiltClass
{
    public class Item
    {
        public string Name { get; set; }
        public int Value { get; set; }
    }
    private List<Item> values = new List<Item>();
    public List<Item> Values { get { return values; } set { values = value; } }
}

static class Program
{
    static void Main()
    {
        RunStaticTest();
        RunDynamicTest();
    }

    static void RunStaticTest()
    {
        Console.WriteLine("-------------------------------------");
        Console.WriteLine(" Serializing StaticallyBuiltClass...");
        Console.WriteLine("-------------------------------------");
        var stat = new StaticallyBuiltClass();

        Serialize(stat.GetType(), stat);

        Console.WriteLine();
    }

    static void RunDynamicTest()
    {
        Console.WriteLine("-------------------------------------");
        Console.WriteLine(" Serializing DynamicallyBuiltClass...");
        Console.WriteLine("-------------------------------------");
        CSharpCodeProvider csProvider = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v3.5" } });

        CompilerParameters csParams = new System.CodeDom.Compiler.CompilerParameters();
        csParams.GenerateInMemory = true;
        csParams.GenerateExecutable = false;
        csParams.ReferencedAssemblies.Add("System.dll");
        csParams.CompilerOptions = "/target:library";

        StringBuilder classDef = new StringBuilder();
        classDef.AppendLine("using System;");
        classDef.AppendLine("using System.Collections.Generic;");
        classDef.AppendLine("");
        classDef.AppendLine("public class DynamicallyBuiltClass");
        classDef.AppendLine("{");
        classDef.AppendLine("    public class Item");
        classDef.AppendLine("    {");
        classDef.AppendLine("        public string Name { get; set; }");
        classDef.AppendLine("        public int Value { get; set; }");
        classDef.AppendLine("    }");
        classDef.AppendLine("    private List<Item> values = new List<Item>();");
        classDef.AppendLine("    public List<Item> Values { get { return values; } set { values = value; } }");
        classDef.AppendLine("}");

        CompilerResults res = csProvider.CompileAssemblyFromSource(csParams, new string[] { classDef.ToString() });

        foreach (var line in res.Output)
        {
            Console.WriteLine(line);
        }

        Assembly asm = res.CompiledAssembly;
        if (asm != null)
        {
            Type t = asm.GetType("DynamicallyBuiltClass");
            object o = t.InvokeMember("", BindingFlags.CreateInstance, null, null, null);
            Serialize(t, o);
        }

        Console.WriteLine();
    }

    static void Serialize(Type type, object o)
    {
        var serializer = new XmlSerializer(type);
        try
        {
            serializer.Serialize(Console.Out, o);
        }
        catch(Exception ex)
        {
            Console.WriteLine("Exception caught while serializing " + type.ToString());
            Exception e = ex;
            while (e != null)
            {
                Console.WriteLine(e.Message);
                e = e.InnerException;
                Console.Write("Inner: ");
            }
            Console.WriteLine("null");
            Console.WriteLine();
            Console.WriteLine("Stack trace:");
            Console.WriteLine(ex.StackTrace);
        }
    }
}

生成以下输出:

-------------------------------------
 Serializing StaticallyBuiltClass...
-------------------------------------
<?xml version="1.0" encoding="IBM437"?>
<StaticallyBuiltClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Values />
</StaticallyBuiltClass>
-------------------------------------
 Serializing DynamicallyBuiltClass...
-------------------------------------
Exception caught while serializing DynamicallyBuiltClass
There was an error generating the XML document.
Inner: The type initializer for 'Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterDynamicallyBuiltClass' threw an exception.
Inner: Object reference not set to an instance of an object.
Inner: null

Stack trace:
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces)
   at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o)
   at Program.Serialize(Type type, Object o) in c:\dev\SerTest\SerTest\Program.cs:line 100

编辑:删除了一些无关的引用程序集

最佳答案

CompilerParameters.GenerateInMemory 更改为 false 即可。我不知道这是否是 XML 序列化过程的限制,但如果将程序集生成到磁盘上的临时位置不是问题,那么这将解决您的问题。

关于c# - XmlSerializer 在序列化动态加载的类型时抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2492175/

相关文章:

java - YAML Mapper 如何在反序列化过程中忽略 yml 文件中的某些字段?

c# - EF有时可以在成功写入后重试吗?

c# - MakeSignature.SignDeferred 和嵌入签名问题 (Itextsharp)

java - 如何使单元测试始终通过?

c# - 从哈希表创建对象 C#

c# - 尝试使用 SetValue 时对象与目标类型不匹配

c# - XML 中的不同值

c# - 使用 XtraGrid Entity Framework 创建主/细节网格

java - 有一种干净的方法可以在 Spring Web API 中将字符串作为 json 返回吗?

C# 自定义序列化/反序列化以及 DeflateStreams