Json.NET和混淆,反序列化不工作 [英] Json.NET and Obfuscation, deserialization not working

查看:277
本文介绍了Json.NET和混淆,反序列化不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想和大家分享一个解决方案,我和我的团队发现克服当您尝试夫妇Json.Net和模糊处理出现的问题。

I would like to share a solution my team and I discovered to overcome the problem that appears when you try to couple Json.Net and Obfuscation.

我的配置:VS2013,C#,.NET 4.0,Json.Net 7.0.1,codeWall /加密混淆

My config: VS2013, C#, .NET 4.0, Json.Net 7.0.1, CodeWall/Crypto Obfuscator.

一切正常,直到我混淆我的code。从那以后,我无法反序列化我的JSON文件(已正确连载!)。

Everything works fine till I obfuscate my code. After that I can't deserialize my json file (that has been serialized correctly!).

推荐答案

我们的解决方案提供了之类的黑客的DefaultSerializationBinder类,你可以简单地选择从源头code和修改的意愿(或提供抽象类SerializationBinder您的自定义覆盖)。这个问题似乎当它试图从组件名称发现正确的组件......在这里混淆不会匹配两个出现。

Our solution provide a "sort of hacking" of the DefaultSerializationBinder class, that you can simply pick from the source code and modify at will (or provide your custom override of the abstract class SerializationBinder). The problem seems to appear when it tries to discover the correct assembly from the assembly name...here obfuscation won't match the two.

下面的方法code,你需要重写:

Here the code of the method you need to rewrite:

private static Type GetTypeFromTypeNameKey(TypeNameKey typeNameKey)
{
    string assemblyName = typeNameKey.AssemblyName;
    string typeName = typeNameKey.TypeName;

    if (assemblyName != null)
    {
        // look, I don't like using obsolete methods as much as you do but this is the only way
        // Assembly.Load won't check the GAC for a partial name
        Assembly assembly = Assembly.LoadWithPartialName(assemblyName);

        if (assembly == null)
        {
            string partialName = assemblyName;
            var elements = assemblyName.Split(',');
            if (elements.Length > 0)
            {
                partialName = elements[0];
            }
            // will find assemblies loaded with Assembly.LoadFile outside of the main directory
            Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
            foreach (Assembly a in loadedAssemblies)
            {
                if (a.GetName().Name == assemblyName || a.FullName == assemblyName || a.GetName().Name == partialName)
                {
                    assembly = a;
                    break;
                }
            }
        }

        if (assembly == null)
        {
            throw new JsonSerializationException(string.Format("Could not load assembly '{0}'.", assemblyName));
        }
        Type type = assembly.GetType(typeName);

        if (type == null)
        {
            throw new JsonSerializationException(string.Format("Could not find type '{0}' in assembly '{1}'.", typeName, assembly.FullName));
        }

        return type;
    }
    else if (typeName != null)
    {
        return Type.GetType(typeName);
    }
    else
    {
        return null;
    }
}

我希望这可以帮助!

I hope this can help!

请下跌自由分享您的想法和其他测试,欢迎!

Please fell free to share your ideas and other tests are welcome!

这篇关于Json.NET和混淆,反序列化不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆