BinaryFormatter.Deserialize “unable to find assembly” after ILMerge

前端 未结 10 1079
囚心锁ツ
囚心锁ツ 2020-12-14 20:53

I have a C# solution with a referenced dll (also C# with the same .Net version). When I build the solution and run the resulting exe, without merging the exe and the refere

10条回答
  •  独厮守ぢ
    2020-12-14 21:20

        public sealed class DeserializationBinder : SerializationBinder
    {
        private readonly string _typeName;
        private readonly Assembly _assembly;
        public DeserializationBinder(Assembly assembly, string typeName)
        {
            _typeName = typeName;
            _assembly = assembly;
        }
    
        public override Type BindToType(string assemblyName, string typeName)
        {
            Type typeToDeserialize = null;
            if (!assemblyName.Contains("System") && !assemblyName.Contains("mscorlib"))
            {
                String currentAssembly = _assembly.FullName;
                assemblyName = currentAssembly;
                typeName = _typeName;
            }
            typeToDeserialize = Type.GetType(String.Format("{0}, {1}",
                typeName, assemblyName));
            return typeToDeserialize;
        }
    }
    

提交回复
热议问题