Eazfuscator. Net 3.3: processing the WPF viewmodel Type Definition

Source: Internet
Author: User

Attribute definitions in viewmodel are used in data binding in XAML, so they cannot be obfuscated. Normally, in eazfuscator. in the. NET 3.3 environment, the viewmode type and the attributes to be bound are defined as public, so that eazfuscator. net will not try to confuse these attributes.

However, this will lead to other problems. First, because the type itself is public, the type name will not be obfuscated, so that the type name may not be exposed. Secondly, because the type is public, some non-attribute members are used inside the Assembly. For example, the internal modifier must be carefully added to the definition of methods and events, because if the type is public, these members will not be confused. Some special members, such as calling the event method, usually need to be defined as protected virtual, And because viewmodel is defined as public, protected also changes members to non-internal members. Therefore, this special method cannot be obfuscated unless the protected modifier is not used, but this obviously breaks the general rule. (Note that protected internal in C # is also a "or" relationship, that is, it represents both protected and internal. Here, we actually need the modifier of protected "and" Internal. Although IL supports it, C # does not .)

In addition, if the viewmodel type is defined in the EXE assembly, you must add the obfuscateassembly feature and set the assemblyisprivate parameter to false to confuse eazfuscator. Net with the public type in the EXE assembly. The following code:

Using system. reflection;

[Assembly: obfuscateassemblyattribute (false)]

 

This is a simple viewmodel sample type defined using the above method:

Public class myviewmodel

{

Public int binding property 1 {Get; private set ;}

Public string binding property 2 {Get; private set ;}

 

Internal void internal method 1 ()

{}

 

Internal event eventhandler myevent;

 

// The Event call method uses the protected modifier, And the type is public, so it cannot be obfuscated.

Protected virtual void onmyevent (eventargs E)

{

VaR handler = This. myevent;

If (handler! = NULL)

Handler (this, e );

}

}

Result After obfuscation using eazfuscator. Net 3.3:

We can see that the type name is not obfuscated, and there is no way to confuse event calling methods.

 

Here we will introduce another method to define this type. We will convert the type to public, and then use system. reflection. the obfuscation feature to command eazfuscator. net 3.3 does not confuse attributes of the type. The specific method is to set the feature attribute to "properties renaming ". In this case, since the type is internal, we can quickly use the public modifier to define all other members.

The preceding type is defined as follows:

[System. reflection. obfuscationattribute (feature = "properties renaming")]

Class myviewmodel

{

Public int binding property 1 {Get; private set ;}

Public string binding property 2 {Get; private set ;}

 

Public void internal method 1 ()

{}

 

Public event eventhandler myevent;

Protected virtual void onmyevent (eventargs E)

{

VaR handler = This. myevent;

If (handler! = NULL)

Handler (this, e );

}

}

After obfuscation:

Very good. Only attributes that do not need to be obfuscated are exposed, and all other information (including type names) is obfuscated. There will be no problems caused by the protected modifier in the preceding event call method.

 

However, there is another small problem. Since the above method disables all attributes in the obfuscation type of eazfuscator. Net 3.3, what if the internal (internal) attribute needs to be defined in viewmodel?

First of all, this problem was not mentioned at first because the attributes in viewmodel are usually used for binding, so this can be done for convenience. Of course, if you really need to define the internal used attributes, that is, the attributes that need to be obfuscated, you can only remove the obfuscation feature in the type definition, instead, the obfuscation is defined on every attribute that does not need to be obfuscated. The following code:

// There is no obfuscation feature on the type name

Class myviewmodel

{

[System. reflection. obfuscationattribute (feature = "Renaming")]

Public int binding property 1 {Get; private set ;}

[System. reflection. obfuscationattribute (feature = "Renaming")]

Public string binding property 2 {Get; private set ;}

// This attribute will be obfuscated

Public int internal attribute {Get; private set ;}

}

 

Update, we found a better way to directly cancel obfuscation of Public attributes, so that the obfuscation of attributes without obfuscation can be directly written as internal!

// Cancel obfuscation of Public attributes

[System. reflection. obfuscation (feature = "apply to member * When property and public: Renaming")]

Class myviewmodel

{

/*...*/

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.