SmartAssembly 6

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 define the attributes, either:

  • in Visual Studio, add a reference to the SmartAssembly.Attributes.dll (located in the %ProgramFiles%\SmartAssembly 6\Attributes folder) in your project.
  • add the source code file to your project (SmartAssembly.Attributes.cs for a C# project, or SmartAssembly.Attributes.vb for a VB.NET project).
    Both of these files are located in the Smartassembly\attributes folder. If you choose this option, you can change the namespace to match your own, simplifying the use of these attributes.  

Attributes for Obfuscation

DoNotObfuscate

Excludes the member from obfuscation.

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

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).

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).

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).

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.

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, and struct.

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.

Code samples

(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 
{ 
} 

(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 

(C#)

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

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

(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?