ObfuscationAttribute 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
指示模糊化工具針對元件、類型或成員採取指定的動作。
public ref class ObfuscationAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Event | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.Struct, AllowMultiple=true, Inherited=false)]
public sealed class ObfuscationAttribute : Attribute
public sealed class ObfuscationAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Event | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.Struct, AllowMultiple=true, Inherited=false)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class ObfuscationAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Event | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.Struct, AllowMultiple=true, Inherited=false)>]
type ObfuscationAttribute = class
inherit Attribute
type ObfuscationAttribute = class
inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Event | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.Struct, AllowMultiple=true, Inherited=false)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ObfuscationAttribute = class
inherit Attribute
Public NotInheritable Class ObfuscationAttribute
Inherits Attribute
- 繼承
- 屬性
範例
以下程式碼範例展示了一個具有兩種類型的公共assembly: Type1 和 Type2。 該組件被標記為混淆,該 ObfuscateAssemblyAttribute標記為公用(即 AssemblyIsPrivate 屬性為 false)。
Type1 標記為混淆,因為該組件本身標記為混淆。 其中 Type1 一成員被排除在混淆之外,使用該 Exclude 性質。
Type2 被排除在混淆之外,但其成員因性質 ApplyToMembers 為 false而標記為混淆。
MethodA方法Type2以該屬性的值"default"Feature標示。 必須指定false屬性以避免被MethodA排除混淆,因為屬性Exclude的預設值為 trueExclude 。 混淆工具在混淆後不應剝除屬性,因為屬性 StripAfterObfuscation 為 false。 本程式碼範例中其他屬性在混淆後被剝除,因為 StripAfterObfuscation 該屬性未被指定,因此預設為 true。
程式碼範例包含顯示屬性及其屬性的程式碼。 你也可以打開帶有 Ildasm.exe(IL 反組譯器)的 DLL 來檢查屬性。
using System;
using System.Reflection;
// Mark this public assembly for obfuscation.
[assembly:ObfuscateAssemblyAttribute(false)]
// This class is marked for obfuscation, because the assembly
// is marked.
public class Type1
{
// Exclude this method from obfuscation. The default value
// of the Exclude property is true, so it is not necessary
// to specify Exclude=True, although spelling it out makes
// your intent clearer.
[ObfuscationAttribute(Exclude=true)]
public void MethodA() {}
// This member is marked for obfuscation because the class
// is marked.
public void MethodB() {}
}
// Exclude this type from obfuscation, but do not apply that
// exclusion to members. The default value of the Exclude
// property is true, so it is not necessary to specify
// Exclude=true, although spelling it out makes your intent
// clearer.
[ObfuscationAttribute(Exclude=true, ApplyToMembers=false)]
public class Type2
{
// The exclusion of the type is not applied to its members,
// however in order to mark the member with the "default"
// feature it is necessary to specify Exclude=false,
// because the default value of Exclude is true. The tool
// should not strip this attribute after obfuscation.
[ObfuscationAttribute(Exclude=false, Feature="default",
StripAfterObfuscation=false)]
public void MethodA() {}
// This member is marked for obfuscation, because the
// exclusion of the type is not applied to its members.
public void MethodB() {}
}
// This class only exists to provide an entry point for the
// assembly and to display the attribute values.
internal class Test
{
public static void Main()
{
// Display the ObfuscateAssemblyAttribute properties
// for the assembly.
Assembly assem = typeof(Test).Assembly;
object[] assemAttrs = assem.GetCustomAttributes(
typeof(ObfuscateAssemblyAttribute), false);
foreach( Attribute a in assemAttrs )
{
ShowObfuscateAssembly((ObfuscateAssemblyAttribute) a);
}
// Display the ObfuscationAttribute properties for each
// type that is visible to users of the assembly.
foreach( Type t in assem.GetTypes() )
{
if (t.IsVisible)
{
object[] tAttrs = t.GetCustomAttributes(
typeof(ObfuscationAttribute), false);
foreach( Attribute a in tAttrs )
{
ShowObfuscation(((ObfuscationAttribute) a),
t.Name);
}
// Display the ObfuscationAttribute properties
// for each member in the type.
foreach( MemberInfo m in t.GetMembers() )
{
object[] mAttrs = m.GetCustomAttributes(
typeof(ObfuscationAttribute), false);
foreach( Attribute a in mAttrs )
{
ShowObfuscation(((ObfuscationAttribute) a),
t.Name + "." + m.Name);
}
}
}
}
}
private static void ShowObfuscateAssembly(
ObfuscateAssemblyAttribute ob)
{
Console.WriteLine("\r\nObfuscateAssemblyAttribute properties:");
Console.WriteLine(" AssemblyIsPrivate: {0}",
ob.AssemblyIsPrivate);
Console.WriteLine(" StripAfterObfuscation: {0}",
ob.StripAfterObfuscation);
}
private static void ShowObfuscation(
ObfuscationAttribute ob, string target)
{
Console.WriteLine("\r\nObfuscationAttribute properties for: {0}",
target);
Console.WriteLine(" Exclude: {0}", ob.Exclude);
Console.WriteLine(" Feature: {0}", ob.Feature);
Console.WriteLine(" StripAfterObfuscation: {0}",
ob.StripAfterObfuscation);
Console.WriteLine(" ApplyToMembers: {0}", ob.ApplyToMembers);
}
}
/* This code example produces the following output:
ObfuscateAssemblyAttribute properties:
AssemblyIsPrivate: False
StripAfterObfuscation: True
ObfuscationAttribute properties for: Type1.MethodA
Exclude: True
Feature: all
StripAfterObfuscation: True
ApplyToMembers: True
ObfuscationAttribute properties for: Type2
Exclude: True
Feature: all
StripAfterObfuscation: True
ApplyToMembers: False
ObfuscationAttribute properties for: Type2.MethodA
Exclude: False
Feature: default
StripAfterObfuscation: False
ApplyToMembers: True
*/
Imports System.Reflection
' Mark this public assembly for obfuscation.
<Assembly: ObfuscateAssemblyAttribute(False)>
' This class is marked for obfuscation, because the assembly
' is marked.
Public Class Type1
' Exclude this method from obfuscation. The default value
' of the Exclude property is True, so it is not necessary
' to specify Exclude:=True, although spelling it out makes
' your intent clearer.
<ObfuscationAttribute(Exclude:=True)> _
Public Sub MethodA()
End Sub
' This member is marked for obfuscation because the class
' is marked.
Public Sub MethodB()
End Sub
End Class
' Exclude this type from obfuscation, but do not apply that
' exclusion to members. The default value of the Exclude
' property is True, so it is not necessary to specify
' Exclude:=True, although spelling it out makes your intent
' clearer.
<ObfuscationAttribute(Exclude:=True, ApplyToMembers:=False)> _
Public Class Type2
' The exclusion of the type is not applied to its members,
' however in order to mark the member with the "default"
' feature it is necessary to specify Exclude:=False,
' because the default value of Exclude is True. The tool
' should not strip this attribute after obfuscation.
<ObfuscationAttribute(Exclude:=False, _
Feature:="default", StripAfterObfuscation:=False)> _
Public Sub MethodA()
End Sub
' This member is marked for obfuscation, because the
' exclusion of the type is not applied to its members.
Public Sub MethodB()
End Sub
End Class
' This class only exists to provide an entry point for the
' assembly and to display the attribute values.
Friend Class Test
Public Shared Sub Main()
' Display the ObfuscateAssemblyAttribute properties
' for the assembly.
Dim assem As Assembly =GetType(Test).Assembly
Dim assemAttrs() As Object = _
assem.GetCustomAttributes( _
GetType(ObfuscateAssemblyAttribute), _
False)
For Each a As Attribute In assemAttrs
ShowObfuscateAssembly(CType(a, ObfuscateAssemblyAttribute))
Next
' Display the ObfuscationAttribute properties for each
' type that is visible to users of the assembly.
For Each t As Type In assem.GetTypes()
If t.IsVisible Then
Dim tAttrs() As Object = _
t.GetCustomAttributes( _
GetType(ObfuscationAttribute), _
False)
For Each a As Attribute In tAttrs
ShowObfuscation(CType(a, ObfuscationAttribute), _
t.Name)
Next
' Display the ObfuscationAttribute properties
' for each member in the type.
For Each m As MemberInfo In t.GetMembers()
Dim mAttrs() As Object = _
m.GetCustomAttributes( _
GetType(ObfuscationAttribute), _
False)
For Each a As Attribute In mAttrs
ShowObfuscation(CType(a, ObfuscationAttribute), _
t.Name & "." & m.Name)
Next
Next
End If
Next
End Sub
Private Shared Sub ShowObfuscateAssembly( _
ByVal ob As ObfuscateAssemblyAttribute)
Console.WriteLine(vbCrLf & "ObfuscateAssemblyAttribute properties:")
Console.WriteLine(" AssemblyIsPrivate: " _
& ob.AssemblyIsPrivate)
Console.WriteLine(" StripAfterObfuscation: " _
& ob.StripAfterObfuscation)
End Sub
Private Shared Sub ShowObfuscation( _
ByVal ob As ObfuscationAttribute, _
ByVal target As String)
Console.WriteLine(vbCrLf _
& "ObfuscationAttribute properties for: " _
& target)
Console.WriteLine(" Exclude: " & ob.Exclude)
Console.WriteLine(" Feature: " & ob.Feature)
Console.WriteLine(" StripAfterObfuscation: " _
& ob.StripAfterObfuscation)
Console.WriteLine(" ApplyToMembers: " & ob.ApplyToMembers)
End Sub
End Class
' This code example produces the following output:
'
'ObfuscateAssemblyAttribute properties:
' AssemblyIsPrivate: False
' StripAfterObfuscation: True
'
'ObfuscationAttribute properties for: Type1.MethodA
' Exclude: True
' Feature: all
' StripAfterObfuscation: True
' ApplyToMembers: True
'
'ObfuscationAttribute properties for: Type2
' Exclude: True
' Feature: all
' StripAfterObfuscation: True
' ApplyToMembers: False
'
'ObfuscationAttribute properties for: Type2.MethodA
' Exclude: False
' Feature: default
' StripAfterObfuscation: False
' ApplyToMembers: True
備註
ObfuscationAttribute與ObfuscateAssemblyAttribute屬性允許組合語言作者註解其二進位檔,使混淆工具能以最小的外部設定正確處理它們。
這很重要
套用此屬性不會自動混淆你套用該屬性的程式碼實體。 套用該屬性是建立混淆工具設定檔的替代方案。 也就是說,它僅提供混淆工具的指令。 Microsoft 建議混淆工具的廠商遵循此處描述的語意。 然而,無法保證某個工具一定遵循 Microsoft 的建議。
該 ObfuscationAttribute 屬性具有字串 Feature 屬性。 混淆工具可以將此屬性的字串值映射到他們實作的功能,最好是透過使用者可存取的 XML 設定檔。 它 ObfuscationAttribute 定義了兩個功能字串,分別是「default」和「all」。 字串「default」應對應工具的預設混淆功能,而「all」則應對應工具所支援的完整混淆功能集合。 屬性的預設值 Feature 為「all」,使得完整的混淆功能組合得以實現。
應用於組件時, ObfuscationAttribute 也適用於組件內所有類型。 若 ApplyToMembers 屬性未指定,或設定為 true,該屬性也適用於所有成員。
ObfuscationAttribute 並未明確規定集會是公開還是私人。 要指定組裝是公開還是私有,請使用屬性。ObfuscateAssemblyAttribute
當應用於類別與結構時,ObfuscationAttribute若未指定屬性或設定為 true,也適用於所有該型別ApplyToMembers的成員。
當該屬性套用於方法、參數、欄位與屬性時,僅影響被套用的實體。
建構函式
| 名稱 | Description |
|---|---|
| ObfuscationAttribute() |
初始化 ObfuscationAttribute 類別的新執行個體。 |
屬性
| 名稱 | Description |
|---|---|
| ApplyToMembers |
取得或設定一個 Boolean 值,表示某型態的屬性是否套用到該型別的成員。 |
| Exclude |
取得或設定一個 Boolean 值,指示混淆工具是否應排除該類型或成員。 |
| Feature |
取得或設定一個字串值,該字串被混淆工具識別,並指定處理選項。 |
| StripAfterObfuscation |
取得或設定一個 Boolean 值,指示混淆工具在處理後是否應移除此屬性。 |
| TypeId |
在衍生類別中實作時,取得這個 Attribute的唯一標識碼。 (繼承來源 Attribute) |
方法
| 名稱 | Description |
|---|---|
| Equals(Object) |
傳回值,這個值表示這個實例是否等於指定的物件。 (繼承來源 Attribute) |
| GetHashCode() |
傳回這個實例的哈希碼。 (繼承來源 Attribute) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| IsDefaultAttribute() |
在衍生類別中覆寫時,指出這個實例的值是否為衍生類別的預設值。 (繼承來源 Attribute) |
| Match(Object) |
在衍生類別中覆寫時,傳回值,指出這個實例是否等於指定的物件。 (繼承來源 Attribute) |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |
明確介面實作
| 名稱 | Description |
|---|---|
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。 (繼承來源 Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
擷取 物件的型別資訊,可用來取得介面的類型資訊。 (繼承來源 Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
擷取物件提供的類型資訊介面數目 (0 或 1)。 (繼承來源 Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
提供物件所公開屬性和方法的存取權。 (繼承來源 Attribute) |