Search code examples
c#obfuscationdotfuscator

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


I have this piece of code in my C# application:

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:

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?

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

Solution

  • 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.