Compiler created anonymous types: breakable behavior

我只是一个虾纸丫 提交于 2019-12-11 07:47:56

问题


I have some client side HTTP RESTfull code which depends on the comfort that .net compiler generated anonymous types provide.

anonymous type example0 :

string prop_1 = "strValue1"; int prop_2 = 5; new { prop1 = prop_1, prop2 = prop_2};

anonymous type example1 :

new { prop1 = "strValue1", prop2 = 5};

anonymous type example2 (also valid):

string prop1 = "strValue1"; int prop2 = 5; new { prop1, prop2};

The problem here is, that the names of the compiler generated anonymous type's properties are very often important and can not be renamed. Specifically if the user has actively declared a property name for the anonymous type it could indicate that the name is actually important -even outside the assembly-

Dotfuscator Community currently by default renames the properties of the anonymous types.

One of such problematic area is the "ad hoc" building of JSON objects for serialization and network transfer. See code below.

This is some code, where I manually assemble an HTTP message content for WebAPI query. The correctness of the request depends on the exact property name. Anonymous type is used to not have to declare new -newer again to be used- types for every type of HTTP WebAPI query. The names username, password and request_token are important outside the application (for the API server)

string baseUrl = BASE_Address + BASE_Path + VALIDATE_REQUEST_TOKEN_W_LOGIN_Path;
var query = new Dictionary<string, string>();
query.Add(API_KEY_Key, _settings.ApiKey);
string requestUri = QueryHelpers.AddQueryString(baseUrl, query);
var jsonObj = new { username = username, password = password, request_token = requestToken };
string json = JsonConvert.SerializeObject(jsonObj);
var content = new StringContent(json, encoding: Encoding.UTF8, mediaType: "application/json");

Expected behavior: If Dotfuscator can not determine that it is safe to rename anonymous properties, then it should not rename it.

Consider:

  1. If the user has actively named the anonymous types property, that it must be suspicious. Note that the sole reason for doing example0 instead of the shorter example2 (see above) is to get safety against type renaming in Visual Studio. With example0 even if you rename the variable prop_1 or prop_2 it will not break the anonymous typename and thus the resulting Json object

  2. If the method or any called sub-method uses serialization than renaming the property surely will break the code. (as the typename in the serialized Json will depend on the original property name as there are no user declared attributes for serialization naming)

Now the question: can You recommend me some attribute or trick that preserves my current code but makes sure that Dotfuscator doesnt rename anything in the method/anonymous type?


回答1:


Note: I am answering here in my professional capacity as an employee of the developer of Dotfuscator

Dotfuscator Community has included a default reference rule for excluding properties from generated anonymous types since version 5.34. (You can download the latest version of Dotfuscator Community from here). Alternatively, you can define the rule manually which is:

Exclude all properties from types named .*AnonymousType.* containing the custom attribute System.Runtime.CompilerServices.CompilerGeneratedAttribute

<type name=".*AnonymousType.*" regex="true" excludetype="false">
    <customattribute name="System\.Runtime\.CompilerServices\.CompilerGeneratedAttribute" regex="true" />
    <propertymember name="*" regex="true" />
</type>


来源:https://stackoverflow.com/questions/57690988/compiler-created-anonymous-types-breakable-behavior

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!