SmartAssembly 8

Using custom attributes


You can use custom attributes to specifically exclude parts of your code from obfuscation, pruning, sealing and error reporting.

Defining attributes

To apply the attributes to your code, add a reference to RedGate.SmartAssembly.Attributes package to your project:

  1. Open "Manage NuGet Packages" window for your project:


  2. Search for "RedGate.SmartAssembly.Attributes" and install it:


  3. You can now use all available attributes, for example:

Adding a NuGet package is a recommended way for applying the attributes.

Alternatively, you can either:

  • in Visual Studio, add a reference to:

    • for .NET Framework: the SmartAssembly.Attributes.dll assembly located in the %ProgramFiles%\Red Gate\SmartAssembly 8\Attributes\ folder.

    • for .NET Core and .NET Standard: the SmartAssembly.Attributes.dll assembly located in the %ProgramFiles%\Red Gate\SmartAssembly 8\Attributes\NetStandard\ folder.

  • add the source code file to your project. If you choose this option, you can change the namespace to match your own, simplifying the use of these attributes.
    • for C# project: SmartAssembly.Attributes.cs located in the %ProgramFiles%\Red Gate\ SmartAssembly 8\Attributes\ folder.
    • for VB .NET projectSmartAssembly.Attributes.vb located in the %ProgramFiles%\Red Gate\ SmartAssembly 8\Attributes\ folder.


Reference to the SmartAssembly.Attributes assembly will be removed once the assembly is built with SmartAssembly. Used attributes will also be removed and will not be visible in the final assembly.

List of available attributes

Attributes for Automatic Error Reporting

ReportException

Reports any unhandled exception, which occurs in this method. This is useful for DLLs because it saves you catching exceptions yourself and passing the exceptions to SmartAssembly.

Can be added to any method.

DoNotCapture

When added to any type (class, enum, interface, or struct), prevents that type's fields from being included in error reports.

When added to a field, prevents that field from being included in error reports.

DoNotCaptureVariables

Prevents variables from being included in error reports.

Can be added to any method.

Attributes for Feature Usage Reporting

ReportUsage

Increments feature usage counter each time the method is run. By default, the method's name is used for the feature name.

You can pass as a featureName  attribute parameter to report the method's usage under a different name.

Can be added to any method.

Attributes for Pruning

DoNotPrune

Excludes the type definition from pruning.

Can be added to any type (class, enum, interface, and struct).

DoNotPruneType

Excludes the type definition, as well as all type's members, from pruning.

Can be added to any type (class, enum, interface, and struct).

Attributes for Obfuscation

DoNotObfuscate

Excludes the member from obfuscation.

Can be added to assembly, class, delegate, enum, field, interface, method, module, and struct.

Note: If you want to exclude enum and all its fields, you should use  [DoNotObfuscateType] attribute for the enum (or add [DoNotObfuscate] to each field).

DoNotObfuscateType

Excludes the type definition, as well as all the type's members from obfuscation.

Can be added to any type (class, enum, interface, and struct).

DoNotMove

Prevents the method from being moved to another type if Method Parent Obfuscation is turned on. Note that methods with any attribute applied is automatically excluded from moving.

Can be added to any method.

DoNotMoveMethods

Prevent all methods in the class from being moved to another type if Method Parent Obfuscation is turned on.

Can be added to any class.

StayPublic

When SmartAssembly obfuscates some members, they may become internal. This can stop other applications from post-processing the obfuscated code. Apply the StayPublicAttribute to ensure that the member remains public after obfuscation.

Can be added to any class, enum, and interface.

ForceObfuscate

Forces element to be obfuscated, even if it was excluded by safety mechanisms. Takes precedence over DoNotObfuscate attribute!

Has an optional useHashAsName parameter. If true, uses MD5 hash of a method name prefixed with _. Otherwise, uses default Name Mangling setting.

Can be added to any property, field, class, method, struct, and interface.

ObfuscateTo

Forces the type or field to be renamed to a name passed as the newName  attribute parameter. Only renames methods when Advanced Renaming Algorithm is used.

Can be added to any type (class, enum, interface, and struct), field, and method.

ObfuscateNamespaceTo

Forces the type to be in a namespace passed as the newName  attribute parameter.

Can be added to any type (class, enum, interface, and struct).

Attributes for Control Flow Obfuscation

ObfuscateControlFlow

This overrides the control flow obfuscation level set in the SmartAssembly project.

When you add this attribute to your assembly, you can set an alternative obfuscation level for 'flagged methods' (methods with the attribute) in the SmartAssembly user interface.

Can be added to any class, method, and struct.

DoNotObfuscateControlFlow

Does not apply Control Flow Obfuscation.

Can be added to any class, method, and struct.

Attributes for References Dynamic Proxy

ExcludeFromMemberRefsProxy 

Turn off References Dynamic Proxy for this member.

Can be added to any method, class, struct, and entire assembly.

Attributes for Strings Encoding

DoNotEncodeStrings

Excludes the member from Strings Encoding. Also excludes all child members if applied to a type.

Can be added to any method, class, struct, and entire assembly.

EncodeStrings

If Strings Encoding is turned on, reverts the effect of DoNotEncodeStrings set on a parent member.

Can be added to any method, class, and struct.

Other attributes

DoNotSealType

Does not seal the type. This overrides the option to automatically seal all possible classes.

Can be added to any type (class, enum, interface, and struct).

Code samples

C#

(C#)

[DoNotSealType()] 
public class MyClass 
{ 
	[DoNotPrune()] 
	[DoNotObfuscate()] 
	public void CalledOnlyByReflection() 
	{ 
	} 
	[DoNotPrune()] 
	public string MyPropertyCalledByReflection 
	{ 
		get 
		{ 
			return "hello world"; 
		} 
	} 
	public void DoSomething([DoNotPrune()] string myString) 
	{ 
	} 
	[ReportException()] 
	[ObfuscateControlFlow()] 
	public void DoSomethingDangerous() 
	{ 
	} 
	public MyClass() 
	{ 
	} 
	[DoNotPrune()] 
	public MyClass(string[] args) 
	{ 
		//Called only by Reflection 
	} 
} 

[DoNotPruneType()] 
[DoNotObfuscateType()] 
public class OtherClass 
{ 
} 

(C#)

[ForceObfuscate]
public class ThisClassShouldBeObfuscated
{
    public ThisClassShouldBeObfuscated() { /* obfuscated constructor */ }

    [ForceObfuscate(true)]
    public void ThisMethodShouldBeObfuscatedUsingHash(string thisParameterShouldBeObfuscatedUsingHash) { }
}

Visual Basic .NET

(VB)

<DoNotSealType()> _ 
Public Class Class1 
	<DoNotPrune(), DoNotObfuscate()> _
	Public Sub CalledOnlyByReflection()
	End Sub
	<DoNotPrune()> _ 
	Public ReadOnly Property 
	MyPropertyCalledByReflection()
		Get 
		Return "hello world" 
		End Get 
	End Property 
	Public Sub DoSomething(<DoNotPrune()> ByVal MyString As String) 
	End Sub 
	<ReportException()> _ 
	<ObfuscateControlFlow()> _ 
	Public Sub DoSomethingDangerous() 
	End Sub 
	Public Sub New() 
	End Sub 
	<DoNotPrune()> _ 
	Public Sub New(ByVal args As String()) 
		REM Called only by Reflection 
	End Sub 
End Class 

<DoNotPruneType(), DoNotObfuscateType()> _ 
Public Class OtherClass 
End Class 

(VB)

<ForceObfuscate>
Public Class ThisClassShouldBeObfuscated
    Public Sub New()
        ' obfuscated constructor
    End Sub

    <ForceObfuscate(True)>
    Public Sub ThisMethodShouldBeObfuscatedUsingHash(ByVal thisParameterShouldBeObfuscatedUsingHash As String)
    End Sub
End Class

Didn't find what you were looking for?