public static dynamic testAnonymousType()
{
var obj = new { Name = "John", Age = 30 };
return obj;
}
The properties Name and Age are also renamed due to the compiler generating internal classes and private fields. This behavior can lead to issues with JSON serialization, as the property names become incorrect.
Since anonymous classes cannot apply attributes, and applying [Obfuscation(Feature = “renaming”, Exclude = true)] for this method does not work, is there a way to prevent such renaming? Or is disabling renaming globally the only solution?
(add [assembly: Obfuscation(Feature = “renaming”, Exclude = true)]
and add [Obfuscation(Feature = “renaming”, Exclude = false, ApplyToMembers = true)] each class → this works)
following code throws Microsoft.CSharp.RuntimeBinder.RuntimeBinderException class XXX does not contain a definition for “Name”
no assembly level renaming Obfuscation attribute
[Obfuscation(Feature = "ultra", Exclude = true)]
public static dynamic testAnonymousType2()
{
var obj = new { Name = "John", Age = 30 };
return obj;
}
public static void testAnonymousType()
{
var obj = testAnonymousType2();
Console.WriteLine($"Name: {obj.Name}, Age: {obj.Age} {obj.ToString()}");
}
public static dynamic testAnonymousType2()
{
var obj = new { Name = "John", Age = 30 };
var converter = new System.Web.Script.Serialization.JavaScriptSerializer();
Console.WriteLine("ToJSON1: " + converter.Serialize(obj));
return obj;
}
In real project, we are in external assembly to do this.
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: “F6B57532” does not contains a defination for “testCallWithDynamicClass”
[assembly: Obfuscation(Feature = "renaming", Exclude = true)]
namespace XXX
{
Class Test{
void Main(){
var ttt = new TestDynamic();
ttt.testCallWithDynamic();
}
}
[Obfuscation(Feature = "renaming", Exclude = false, ApplyToMembers = true)]
public class TestDynamic
{
static void testCallWithDynamicClass(string xxx)
{
Console.WriteLine("testCallWithDynamicClass: " + xxx);
}
public void testCallWithDynamic()
{
dynamic d = new { Name = "abc" };
testCallWithDynamicClass(d.Name); /* because C# compiler use CallSite to make this call and uses string "testCallWithDynamicClass" to call the method, which is renamed by VMP. */
}
}
}
if we dont add [assembly: Obfuscation(Feature = “renaming”, Exclude = true)] and [Obfuscation(Feature = “renaming”, Exclude = false, ApplyToMembers = true)] on the class
there is also RuntimeBinderException: does not contains a defination for “Name”
Force renaming means that all symbols (including methods) pinned to this attribute will be renaming even the methods were used for dynamic calls. Please don’t use this feature if you are not sure that you need it.
P.S. “[assembly: Obfuscation…” means that you use this attribute for all objects in the assembly by default. Obfuscation attributes specified for class/method/field/property override attributes of outer classes/assembly.
Thank you for the clarification. We are using the renaming attribute solely as a workaround for the main issue at original post. However, the current problem persists even without any renaming tags, as dynamics are still encountering issues.
so the problem is RuntimeBinderException and JSON property has wrong name due to compiler generated class is internal and fields are private.