BinaryFormatter deserialize gives SerializationException

前端 未结 6 949
别跟我提以往
别跟我提以往 2020-12-16 15:28

I\'m getting an:

System.Runtime.Serialization.SerializationException: Unable to find assembly \'myNameSpace, Version=1.0.0.0, Culture=neutral, Pub

6条回答
  •  隐瞒了意图╮
    2020-12-16 16:20

    I ran into a similar problem and I got it working with help of the following link: BinaryFormatterDeserialize-not-finding-a-type

    Basically what you need to do is subscribe to the AssemblyResolve event BEFORE deserializing. Then unsubscribe after deserialization..

    AppDomain.CurrentDomain.AssemblyResolve +=
                    new ResolveEventHandler(CurrentDomain_AssemblyResolve);
    // CODE TO DESERIALIZE HERE
    
    AppDomain.CurrentDomain.AssemblyResolve -= new ResolveEventHandler(CurrentDomain_AssemblyResolve);
    

    Here the method I used to resolve the Assembly:

    static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        try
        {
            if(args.Name == "MY ASSEMBLY NAME"))
            {
                //Load my Assembly 
                Assembly assem = Assembly.LoadFrom("MY ASSEMBLY PATH");
                if(assem != null)
                    return assem;
            }
        }
        catch { ;}
    
        return Assembly.GetExecutingAssembly();
    }
    

提交回复
热议问题