Today, we will focus on data binding, but first, let’s define it. Data binding allows Windows Forms applications to display and update UI controls from a data source without having to modify the control’s source code.
When protecting Windows Forms applications, it is important to note how the data-bound controls are constructed to determine if they will be impacted by code obfuscation. If the controls bind to a collection of objects, the original property names of that object must be preserved to correctly populate the “DisplayMember” and “ValueMember” properties of the control. When binding controls to an Enum, the original names of its members must be preserved, or the GUI control might show obfuscated names. On the other hand, if we’re binding directly to a database table (and the table does not map to an object in source code), we don’t need any custom configurations because Dotfuscator does not mangle table and column names.
This simple Windows Forms application has three UI controls with different data binding techniques: a DataGridView binds to a Customer table in a database, a ListBox binds to a collection of Employee objects, and ComboBox binds to an Enum called DaysOfWeek:
If I obfuscate with project defaults, I experience a runtime error at app startup:
This occurs because the original property names of the Employee object are used in the “DisplayMember” and “ValueMember” ListBox properties:
listBox1.DataSource = employeeList;
listBox1.DisplayMember = “Name”;
listBox1.ValueMember = “Department”;
First, I’ll open my project configuration file (DotfuscatorConfig.xml) in the Dotfuscator Config Editor and set a Rename exclusion for the properties in the Employee object:
After configuring these Rename exclusions, the application starts without the runtime exception, but the “DaysOfWeek” ComboBox appears with obfuscated names:
To fix this, I will configure a Rename exclusion for the members of DaysOfWeek.
After providing this Rename exclusion, the app starts without issues or erroneous behavior. Please also note the DataGridView, which binds to the Customer table in our database, did not require any Rename configuration to start and display correctly.
There are several different ways to use data binding in Windows Forms applications. We’ve seen a few ways that obfuscation can impact data-bound controls. If you experienced a runtime crash or erroneous UI behavior after applying obfuscation, please use the above steps to resolve the issue.
The full example can be downloaded here.
If you have any feedback on this topic or other topics you would like us to discuss in the Support Corner, please contact our Support Department.