Workaround for Reflection Bug in Dotfuscator?

▼魔方 西西 提交于 2019-12-23 12:46:15

问题


Greetings all,

I am calling Type.GetProperties(), but after running Dotfuscator, it is returning zero items, when it returned more than zero before.

public class Test
{
    public int Number { get; set; }

    public void ShowInfo()
    {
        Type type = this.GetType();

        PropertyInfo[] props = type.GetProperties();
        Console.WriteLine("type [" + type.Name + "] props count: " + props.Length);
    }
}

If I exclude the "Number" property from renaming within Dotfuscator, then it works, but otherwise it doesn't. However, it is not possible for me to do this for all properties in my project, as it would lead to possible bugs.

Are there any workarounds for this method? Or even other "free" obfuscation applications I could use?

I have already tried looking on their website to submit a bug, but I am only using the community edition so there doesn't seem to be as much support for it.


回答1:


Dotfuscator automatically strips properties (which are just metadata anyway - the real work is done by the get/set pair of methods that are automatically created) during renaming. It also renames the underlying get/set methods as well. Depending on what you are trying to do, you'll need to exclude either the property metadata itself, or the get/set methods (or possibly both) from renaming.

If you need to keep the property metadata intact (for example, to simply list the properties in a Type), you can instruct Dotfuscator to exclude properties from renaming by checking them in the tree view on the Renaming Exclusions tab or using a custom regex property rule. This will only exclude the property metadata - the get/set methods will still be renamed.

If you need to keep the get/set methods (because, for example, you are trying to get or set a property's value by reflection), you can instruct Dotfuscator to exclude those methods from renaming by expanding the property in the tree view and checking the get/set methods underneath, or by using a custom regex method rule.




回答2:


As the process of obfuscation is not limited to renaming your class members, you can't be sure of that. That's the problem with obfuscation: You basically can't make any assumptions about your class anymore regarding the result of reflection. The only way I can think of is to not use reflection but expressions.

Have a look at this question and its answer to know, what I mean with "expressions": How to raise PropertyChanged event without using string name



来源:https://stackoverflow.com/questions/6203453/workaround-for-reflection-bug-in-dotfuscator

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