SmartAssembly - 6.7
Pruning unused code - SmartAssembly
Pruning removes code that will never be executed at runtime. Pruning also removes metadata such as design attributes and the names of events and properties.
Pruning reduces the size of your assemblies, and improves loading time and performance. Removing non-useful metadata can protect your code by making it more difficult to understand.
To prune code
To prune code, in the Project Settings window, go to Pruning or click the icon in the toolbar. |
|
Select the assemblies to prune.
When pruning is enabled, the colored bars under the toolbar icon and to the left of the features options are green. If it is disabled, the bars are orange.

Pruning example
The following example shows code before and after pruning:
Before pruning:
[DefaultMember("Item")]
public sealed class DiscoveryClientResultCollection : CollectionBase
[XmlRoot("discovery", Namespace="http://schemas.xmlsoap.org/disco/")]
public sealed class DiscoveryDocument
public class DiscoveryDocumentLinksPattern : DiscoverySearchPattern
[XmlRoot("discoveryRef", Namespace="http://schemas.xmlsoap.org/disco/")]
public sealed class DiscoveryDocumentReference : DiscoveryReference
public sealed class DiscoveryDocumentSearchPattern : DiscoverySearchPattern
internal TraceMethod(object target, string name, params object[] args);
internal static string CallString(object target, string method, params object[] args);
private static string HashString(object objectValue);
internal static string MethodId(object target, string method);
public override string ToString();
private static void WriteObjectId(StringBuilder sb, object o);
private static void WriteValue(StringBuilder sb, object o);
After pruning:
[DefaultMember("Item")]
public sealed class DiscoveryClientResultCollection : CollectionBase
public sealed class DiscoveryDocumentSearchPattern : DiscoverySearchPattern
internal TraceMethod(object 1, string 1, params object[] strings);
private static string HashString(object 1);
internal static string MethodId(object 1, string 1);
Troubleshooting pruning
If your application fails after pruning, this is probably because a member that was pruned is called by reflection (or any technology based on reflection, for example serialization, remoting or data-binding).
SmartAssembly usually detects that a member cannot be pruned. However, in some cases, you need to exclude the member from pruning manually.
Note: the member that you need to exclude is the member called by reflection, not the calling member itself.
To determine which member to exclude from the pruning, you can:
- process your application without obfuscation. In case of error, use the non-obfuscated stack-trace to help you locate where the pruned member is called.
- use ASCII name mangling (instead of Unicode) for the obfuscation. This allows you to decode the stack-trace and retrieve the method where the member is called (this procedure is explained in Troubleshooting after building).
- use the error reporting feature.
Excluding members from pruning
Members excluded from pruning will be included in the resulting assembly.
To exclude members from pruning:
- On the project settings page, under Pruning, click Exclusions
- Navigate to the assembly members that you want to exclude from pruning.
- Use the checkboxes to exclude the members, then click Exclude selected:

If SmartAssembly has determined that the whole member can be pruned, instead choose whether to exclude only the definition or the whole member from pruning, then click Exclude from pruning:

- Click Apply
If you are using Linq, you must exclude any classes using the System.Data.Linq.Mapping.Table attribute from obfuscation and pruning (this also applies if they are used as a dependency). Depending on the specific application you many need to exclude other classes.
Excluding public members from pruning
Executable files
By default, SmartAssembly prunes all members of executable files, including public members.
To override this behavior, manually edit the SmartAssembly project file (*.saproj) in an XML editor as follows.
- Locate the
<Assembly>element for the assembly to which you want to apply the setting. - Add the
KeepPublicMembersAccessible="1"attribute to the<Merging>element, as in the example below.
<Assembly AssemblyName="...">
<Merging KeepPublicMembersAccessible="1">
<!-- Settings for the assembly -->
</Merging>
</Assembly>
This enables you to treat executable files in a similar way to DLL files.
Note that the ExcludePublicMembers attribute on the <Pruning> element was replaced by KeepPublicMembersAccessible in SmartAssembly 6.7. Because the new attribute is set on the <Merging> element, the assembly's public members are neither pruned nor obfuscated.
You can also use the [DoNotPrune] and [DoNotPruneType] attributes in your source code. For more information, see Using custom attributes.
DLL files
SmartAssembly does not prune public members of DLL files, because the DLL will be unusable.
Was this article helpful?
SmartAssembly
- Using Smartassembly to obfuscate a windows service
- Obfuscation not renaming identically-named methods in different classes
- Serialization exceptions occurring in obfuscated assemblies
- SmartAssembly skipping obfuscation of some classes in your assembly
- SmartAssembly is not merging or embedding all assembly dependencies
- Can I customize the path to the MDB database?
- System.InvalidOperationException when attempting to connect to local SmartAssembly database
- Protecting website code using SmartAssembly
- Visual Studio Deployment Projects including unprotected builds
- SmartAssembly MSBUILD tasks failing because of difference in log4net assembly
- SmartAssembly Error Reporting: This application has submitted too many reports
- The assembly is being merged, but the dependent assembly isn't
- ERR 2002: Server did not recognize the value of HTTP Header SOAPAction
- The error report is not associated with a valid project ID - SmartAssembly
- Application built with an evaluation edition of SmartAssembly
- How end users can change their participation in Feature Usage Reporting
- Log file for SmartAssembly
- Moving SmartAssembly to another computer
- 'Using JET databases is not possible in 64-bit applications' error when using MSBuild or TFS
- SmartAssembly stack trace is invalid when Method Parent Obfuscation is enabled
- Upgrading SmartAssembly version 4.x to version 5
- Upgrading SmartAssembly Standard to Professional
- Problems building WPF applications with SmartAssembly 6.7
all products
- Some Red Gate products identified as containing a trojan by Anti-Virus software
- Activation may fail with Unknown Error -1
- Product uses web help although a CHM file is available locally
- Argument exception resulting from missing environment variable
- Check for updates may fail when used through proxies
- 'Unidentified Publisher' error when repairing or uninstalling
- Licensing activates product as standard edition
- Moving Red Gate software products to another machine
- Red Gate tools log locations
- The application UI opening slowly when there is no internet access
SmartAssembly
all products
- Red Gate product acknowledgements
- Activating your products
- Activating your products
- Red Gate bundle history
- Check for updates
- Troubleshooting Check for Updates errors
- Current versions
- Deactivating your products
- Installing Red Gate products from the .msi file
- Requesting additional activations
- Serial numbers for bundles
- Reactivating using a different serial number
- Extending your trial
- Finding your serial numbers
- Moving a serial number from one computer to another
- No response received for manual activation
- Licensing and activation resources
- Licensing and activation resources
- Troubleshooting licensing and activation errors
- Licensing and activation FAQs
- Red Gate tools log file locations
- Download old versions of products
- Download product prerequisites & utilities
- Support & upgrades
- Upgrading your software
- Upgrading FAQs


Using SmartAssembly for obfuscation