编译器创建匿名类型:易碎的行为 [英] Compiler created anonymous types: breakable behavior

查看:85
本文介绍了编译器创建匿名类型:易碎的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些客户端HTTP RESTfull代码,这取决于.net编译器生成的匿名类型提供的舒适性.

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

匿名类型example0:

anonymous type example0 :

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

匿名类型example1:

anonymous type example1 :

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

匿名类型example2(同样有效):

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社区当前默认情况下重命名匿名类型的属性.

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

这类有问题的领域之一是特设"构建JSON对象以进行序列化和网络传输.参见下面的代码.

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

这是一些代码,在这里我手动为WebAPI查询组装HTTP消息内容.请求的正确性取决于确切的属性名称.匿名类型用于不必为每种HTTP WebAPI查询类型再次声明new -newer要使用的类型.用户名,密码和request_token名称在应用程序外部(对于API服务器)很重要

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

预期的行为: 如果Dotfuscator无法确定重命名匿名属性是安全的,则不应重命名.

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

考虑:

  1. 如果用户已主动命名匿名类型属性,则该属性必须是可疑的.请注意,执行example0而不是较短的example2(参见上文)的唯一原因是为了防止在Visual Studio中进行类型重命名.使用example0,即使您重命名了变量prop_1或prop_2,它也不会破坏匿名类型名,因此不会破坏生成的Json对象

  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

如果该方法或任何调用的子方法使用序列化,那么重命名该属性肯定会破坏代码. (因为序列化的Json中的类型名称将取决于原始属性名称,因为没有用户声明的属性来进行序列化命名)

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)

现在的问题:您能为我推荐一些属性或技巧来保留我当前的代码,但确保Dotfuscator不会重命名方法/匿名类型中的任何内容吗?

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?

推荐答案

注意:我在此以Dotfuscator开发人员的专业身份进行回答

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

Dotfuscator社区已包含一个默认引用规则,该规则从5.34版本开始就从生成的匿名类型中排除属性. (您可以从此处下载最新版本的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:

从名为.*AnonymousType.*的类型中排除包含自定义属性System.Runtime.CompilerServices.CompilerGeneratedAttribute

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>

这篇关于编译器创建匿名类型:易碎的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆