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

C#-如何在Dotfuscator.Net中使用匿名类型?,c#,obfuscation,dotfuscator,C#,Obfuscation,Dotfuscator,我的C#应用程序中有一段代码: JObject personJson = JObject.FromObject( new { name = "John Doe", age = 34, height = 1.78, weight = 79.34 }); Console.WriteLine(person); 它记录了: { "name": "John Doe", "age": 34,

我的C#应用程序中有一段代码:

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

Console.WriteLine(person);
它记录了:

{
    "name": "John Doe",
    "age": 34,
    "height": 1.78,
    "weight": 79.34
}
Dotfuscate将其混淆为:

Console.WriteLine((object) JObject.FromObject((object) new global::b<string, int, double, double>("John Doe", 34, 1.78, 79.34)));
如何在Dotfuscator中使用匿名类而不出现此问题

编辑:

完整代码:

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);
    }
}

我知道没有人回复你的帖子,所以我想我会用一些想法来回复。你可能已经知道这些事情了,所以我提前道歉

首先,我从模糊代码中看到,从
JObject.FromObject
返回的对象被强制转换为
object
类型。请记住,如果将任何对象引用传递给
控制台.WriteLine
方法,将调用该对象的默认
ToString
方法。因此,在您的示例中调用了
Object.ToString()
方法。在
Object.ToString()
的文档中,它指出:

Object.ToString方法的默认实现返回对象类型的完全限定名

我想说的是,你使用匿名类型在某种程度上混淆了我不知道的事情;但是您能为
JObject
类型编写一个自定义的
ToString
扩展方法吗?可能类似于:

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. 
    }
}
然后调用
Console.WriteLine(JSONObject.ToJSONString())顺便说一句,使用
JSONObject
作为变量名让我感到困惑,因为它看起来像
类型
;你能改用
jsonObject


我希望其他人能把事情弄清楚一点。祝你好运

您/我可以使用动态对象,如下所示:

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);

它看起来很奇怪,当它的模糊,但它确实工作。输出与预期完全一致。

看起来Dotfuscator正在删除属性,即使您不希望它这样做。(这样做是因为它通常是无害的,而且会使逆向工程更加困难。)您应该能够通过配置与
编译器生成的属性
匹配的排除规则,从重命名中排除这些属性,这将防止删除这些属性。这将防止删除匿名类上的所有此类属性

下面是一个项目文件(段)的示例:

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

您可以通过或中的GUI了解如何执行此操作


充分披露:我为抢先解决方案工作。

JObject
不是匿名类型,而
ToString
是一种虚拟方法。问题是dotFuscator将匿名对象(从中构造
JObject
)转换为一个公共的helper泛型类,从而去掉所有属性名;反射帮不了你-这就是
JObject.FromObject
首先使用的东西。谢谢你提供的信息。我有一种预感,我的帖子可能会引起一些回应。我确实说过我在未知的水域里行走。你是说扩展方法不起作用,你对UUioP还有什么进一步的建议吗?恐怕除了配置dotFuscator之外,没有别的办法了——我不知道这会给你什么样的选择,很遗憾:)@RETierney,谢谢你回答我的问题,但问题不在于
JObject.FromObject()
因为
Console.WriteLine(新的{name=“John Doe”,年龄=34,身高=1.78,体重=79.34})编译到
Console.WriteLine((对象)新的全局::b(“JohnDoe”,34,1.78,79.34))谢谢!但是你能给我一个更具体的例子来说明我的情况吗?我对Dotfuscator没有太多的经验。请查看以找到正确的屏幕。在该屏幕上,点击“添加类型”添加类型规则,然后为其命名为
*
。右键单击树中的类型,然后单击“添加自定义属性”。将其命名为
*CompilerGeneratedAttribute
。再次右键单击该类型,然后单击“添加属性”,并将其命名为
*
。保存并构建。
<excludelist>
  <type name=".*" regex="true" excludetype="false">
    <customattribute name=".*CompilerGeneratedAttribute" regex="true" />
    <propertymember name=".*" regex="true" />
  </type>
</excludelist>