简体   繁体   English

C#-如何在Dotfuscator.Net中使用匿名类型?

[英]C# - How to use anonymous types with Dotfuscator.Net?

I have this piece of code in my C# application: 我的C#应用​​程序中包含以下代码:

JObject personJson = JObject.FromObject(
    new
    {
        name = "John Doe",
        age = 34,
        height = 1.78,
        weight = 79.34
    });

Console.WriteLine(person);

And it logs: 它记录:

{
    "name": "John Doe",
    "age": 34,
    "height": 1.78,
    "weight": 79.34
}

And the Dotfuscater obfuscates it to this: 而Dotfuscater将其混淆为:

Console.WriteLine((object) JObject.FromObject((object) new global::b<string, int, double, double>("John Doe", 34, 1.78, 79.34)));

And then the output is this: 然后输出是这样的:

{}

How can I use anonymous classes with the Dotfuscator without this problem? 如何在Dotfuscator中使用匿名类而不会出现此问题?

EDIT: 编辑:

Full code: 完整代码:

public static class Example
{
    static void LogPerson()
    {
        JObject personJson = JObject.FromObject(
            new
            {
                name = "John Doe",
                age = 34,
                height = 1.78,
                weight = 79.34
            });
        Console.WriteLine(JSONObject);
    }
}

I see that no one has replied to your post, so I thought I would respond with some thoughts. 我看到没有人回复您的帖子,所以我想我会有所想法。 You probably already know these things, so I apologize ahead of time. 您可能已经知道这些事情,所以提前致歉。

First, I see that from the obfuscated code, that the returned object from JObject.FromObject is being cast to an object type. 首先,我从混淆代码中看到,从JObject.FromObject返回的对象被JObject.FromObject转换为object类型。 Remember that if you pass any object reference to the Console.WriteLine method, the object's default ToString method will be called. 请记住,如果将任何对象引用传递给Console.WriteLine方法,则将调用该对象的默认ToString方法。 Consequently, Object.ToString() method is being called in your example. 因此,在您的示例中将调用Object.ToString()方法。 From the MSDN documentation for Object.ToString() it states: Object.ToString()MSDN文档中,它指出:

Default implementations of the Object.ToString method return the fully qualified name of the object's type. Object.ToString方法的默认实现返回对象类型的标准名称。

I would say that your use of an anonymous type is confusing things in a way I do not know of; 我想说的是,您对匿名类型的使用以一种我不知道的方式使事情变得混乱。 but could you write a custom ToString extension method for the JObject type? 但是您可以为JObject类型编写自定义ToString扩展方法吗? Maybe something akin to: 也许类似于:

public static class Extensions
{
    public static string ToJSONString(this JObject jo)
    {
        // You could step into this method in the VS debugger to
        // see what 'jo' looks like. You may have to use reflection
        // to get at the properties, but I've never tried it on an 
        // anonymous type. 
    }
}

You would then call Console.WriteLine(JSONObject.ToJSONString()); 然后,您将调用Console.WriteLine(JSONObject.ToJSONString()); BTW, the use of JSONObject as the name of a variable confuses me because it looks like a Type ; 顺便说一句,使用JSONObject作为变量名使我感到困惑,因为它看起来像Type could you use jsonObject instead? 可以改用jsonObject吗?

I hope someone else can clarify things a bit more. 我希望其他人可以澄清更多。 Good Luck! 祝好运!

You/I could use a dynamic object, like this: 您/我可以使用动态对象,如下所示:

dynamic person = new ExpandoObject();
person.name = "John Doe";
person.age = 34;
person.height = 1.78;
person.weight = 79.34;

JObject personJson = JObject.FromObject(person);

Console.WriteLine(personJson);

It looks very weird when it's obfuscated but it does work. 混淆时看起来很奇怪,但确实有效。 The output is exactly as expected. 输出完全符合预期。

It looks like Dotfuscator is removing the properties even though you don't want it to. 看起来Dotfuscator正在删除属性,即使您不希望这样做。 (It does that because usually it's harmless, and it makes reverse-engineering harder.) You should be able to exclude those properties from renaming, which will prevent them from being removed, by configuring an exclude rule that matches the CompilerGeneratedAttribute . (这样做是因为通常它是无害的,并且使反向工程变得更加困难。)您应该能够通过配置与CompilerGeneratedAttribute相匹配的排除规则来排除这些属性的重命名,这将防止它们被删除。 That will prevent all such properties on anonymous classes from being removed. 这样可以防止删除匿名类上的所有此类属性。

Here's an example of a project file (segment) that would do this: 这是一个可以执行此操作的项目文件(段)的示例:

<excludelist>
  <type name=".*" regex="true" excludetype="false">
    <customattribute name=".*CompilerGeneratedAttribute" regex="true" />
    <propertymember name=".*" regex="true" />
  </type>
</excludelist>

You can read about how to do this via the GUI in the Community Edition docs or Pro docs . 您可以通过Community Edition文档Pro文档中的GUI了解如何执行此操作。

Full disclosure: I work for PreEmptive Solutions. 全面披露:我为PreEmptive Solutions工作。

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

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