简体   繁体   English

使用反射进行C#反序列化

[英]C# deserialization using reflection

I have a .dll that deserializes a class. 我有一个.dll反序列化一个类。 When I call this .dll from a project or not using reflection it works fine. 当我从一个项目调用这个.dll或不使用反射时,它工作正常。 When I call the .dll using reflection I get an error on the line that deserializes. 当我使用反射调用.dll时,我在反序列化的行上出现错误。 I know this is due isolation that happens when I use reflection to load an assembly. 我知道这是因为当我使用反射来加载程序集时发生的隔离。 Wondering if anyone has any fix or an idea of how to implement this? 想知道是否有人有任何修复或如何实现这个? BTW, the serialization works just fine, it's just the deserialization that doesnt work. 顺便说一句,序列化工作正常,它只是反序列化不起作用。

I tried both binary and xml, here's the code: 我尝试了二进制和xml,这是代码:

static public object SerializeLoad(string sFilename)
        {
            try
            {
                List<ElementTodo> _object = null;//ElementTodo _object = null;
                Stream stream = File.Open(sFilename, FileMode.Open);
                //BinaryFormatter bformatter = new BinaryFormatter();
                XmlSerializer bformatter = new XmlSerializer(typeof(ElementTodo), "ToDo");

                //_object = (_object.GetType())bformatter.Deserialize(stream);

                _object = (List<ElementTodo>)bformatter.Deserialize(stream);
                stream.Close();
                return _object;
            }
            catch(Exception e)
            {
                string error = e.Message;
                return null;
            }
        }

The generated XML is as follows: 生成的XML如下:

< <

?xml version="1.0"?> 
  <ArrayOfElementTodo xmlns:xsi="w3.org/2001/XMLSchema-instance"; xmlns:xsd="w3.org/2001/XMLSchema"; xmlns="ToDo"> 
      <ElementTodo Title="a" content="aa" isDone="false" /> 
      <ElementTodo Title="b" content="bb" isDone="false" /> 
      <ElementTodo Title="c" content="cc" isDone="false" /> 
      <ElementTodo Title="d" content="dd" isDone="false" /> 
  </ArrayOfElementTodo>

I assume that ElementTodo is in an assembly that both your code and the assembly loaded using reflection have access to? 我假设ElementTodo在一个程序集中,你的代码和使用反射加载的程序集都可以访问? What you have to be careful of is that your loaded assembly is using the same dependent assembly and doesn't load a new copy. 您必须注意的是,加载的程序集使用相同的从属程序集,并且不加载新副本。 Otherwise you wind up with fun errors like 'Object X (of type ElementTodo) is not of type ElementTodo', since two copies of the types are loaded. 否则你会遇到有趣的错误,例如'对象X(类型为ElementTodo)不属于ElementTodo类型',因为加载了两个类型的副本。 It's hard to say for sure that this is your issue without more information on the specific error, however. 但是,如果没有关于特定错误的更多信息,很难确定这是你的问题。

If this is your problem, you can address it by forcing assemblies to resolve to the version that's already loaded using something like this: 如果这是您的问题,您可以通过强制程序集解析为已经加载的版本来解决它:

In your startup code somewhere: 在你的启动代码中的某个地方:

        //This is required because we load assemblies at runtime
        //If this is not used, there can be problems when Reflecting over Types
        AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

Implementation: 执行:

    private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        return AppDomain.CurrentDomain.GetAssemblies().
            FirstOrDefault(assembly => assembly.FullName == args.Name);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM