ObfuscationAttribute 类

定义

指示模糊处理工具对程序集、类型或成员采取指定的操作。Instructs obfuscation tools to take the specified actions for an assembly, type, or member.

public ref class ObfuscationAttribute sealed : Attribute
[System.Runtime.InteropServices.ComVisible(true)]
[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
type ObfuscationAttribute = class
    inherit Attribute
Public NotInheritable Class ObfuscationAttribute
Inherits Attribute
继承
ObfuscationAttribute
属性

示例

下面的代码示例演示具有两个类型的公共程序Type1Type2:和。The following code example shows a public assembly with two types: Type1 and Type2. 该程序集标记为与ObfuscateAssemblyAttribute的模糊处理,后者将程序集标记为公共(即AssemblyIsPrivate ,属性为false)。The assembly is marked for obfuscation with the ObfuscateAssemblyAttribute, which marks the assembly to be treated as public (that is, the AssemblyIsPrivate property is false).

Type1标记为要进行模糊处理,因为程序集标记为要进行模糊处理。Type1 is marked for obfuscation because the assembly is marked for obfuscation. 使用属性从模糊处理中排除一个成员Type1ExcludeOne member of Type1 is excluded from obfuscation, using the Exclude property.

Type2排除了模糊处理,但其成员被标记为模糊处理, ApplyToMembers因为属性false为。Type2 is excluded from obfuscation, but its members are marked for obfuscation because the ApplyToMembers property is false.

MethodA "default"方法标记有属性的值。Feature Type2The MethodA method of Type2 is marked with the value "default" for the Feature property. 需要为false Exclude属性指定以避免排除MethodA模糊处理,因为Exclude属性的默认值为trueIt is necessary to specify false for the Exclude property to avoid excluding MethodA from obfuscation, because the default for the Exclude property is true. 模糊处理工具不应在模糊处理后去除特性, StripAfterObfuscation因为属性false为。The obfuscation tool should not strip the attribute after obfuscation because the StripAfterObfuscation property is false. 此代码示例中的所有其他属性将在模糊处理后被去除StripAfterObfuscation ,因为未指定该属性,因此默认true为。All the other attributes in this code example are stripped after obfuscation, because the StripAfterObfuscation property is not specified, and therefore defaults to true.

此代码示例包含用于显示特性及其属性的代码。The code example includes code to display the attributes and their properties. 还可以通过使用Ildasm (IL 拆装器)打开 DLL 来检查属性。You can also examine the attributes by opening the DLL with the Ildasm.exe (IL Disassembler).

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

注解

ObfuscationAttributeObfuscateAssemblyAttribute特性使程序集作者能够批注其二进制文件,以便模糊处理工具可以通过最少的外部配置正确处理它们。The ObfuscationAttribute and ObfuscateAssemblyAttribute attributes allow assembly authors to annotate their binaries so that obfuscation tools can process them correctly with minimal external configuration.

重要

应用此属性不会自动对应用该属性的代码实体进行模糊处理。Applying this attribute does not automatically obfuscate the code entity to which you apply it. 应用属性是创建模糊处理工具的配置文件的替代方法。Applying the attribute is an alternative to creating a configuration file for the obfuscation tool. 也就是说,它只为模糊处理工具提供说明。That is, it merely provides instructions for an obfuscation tool. Microsoft 建议模糊处理工具的供应商遵循此处所述的语义。Microsoft recommends that vendors of obfuscation tools follow the semantics described here. 但是,并不保证特定工具遵循 Microsoft 建议。However, there is no guarantee that a particular tool follows Microsoft recommendations.

特性有一个字符串Feature属性。 ObfuscationAttributeThe ObfuscationAttribute attribute has a string Feature property. 模糊处理工具可以将此属性的字符串值映射到其实现的功能,最好是使用用户可以访问的 XML 配置文件。Obfuscation tools can map the string values of this property to features they implement, preferably by using an XML configuration file that users can access. ObfuscationAttribute定义了两个功能字符串: "default" 和 "all"。The ObfuscationAttribute defines two feature strings, "default" and "all". 字符串 "default" 应映射到工具的默认模糊处理功能,而 "所有" 应映射到工具支持的一组完整的模糊处理功能。The string "default" should map to the default obfuscation features of a tool, and "all" should map to the complete set of obfuscation features supported by a tool. Feature属性的默认值为 "all",从而启用一组完整的模糊处理功能。The default value of the Feature property is "all", enabling the complete set of obfuscation features.

当应用于程序集时ObfuscationAttribute ,也适用于程序集中的所有类型。When applied to an assembly, ObfuscationAttribute also applies to all types within the assembly. 如果未ApplyToMembers指定该属性,或者将其设置为true,则该属性也适用于所有成员。If the ApplyToMembers property is not specified, or is set to true, the attribute applies to all members as well. ObfuscationAttribute不指定程序集是公共的还是私有的。ObfuscationAttribute does not specify whether an assembly is public or private. 若要指定程序集是公共的还是私有的, ObfuscateAssemblyAttribute请使用特性。To specify whether an assembly is public or private, use the ObfuscateAssemblyAttribute attribute.

当应用于类和结构时ObfuscationAttribute ,如果未指定该ApplyToMembers属性或将其设置为true,则还适用于该类型的所有成员。When applied to classes and structures, ObfuscationAttribute also applies to all members of the type if the ApplyToMembers property is not specified, or is set to true.

当应用于方法、参数、字段和属性时,该属性仅影响应用它的实体。When applied to methods, parameters, fields, and properties, the attribute affects only the entity to which it is applied.

构造函数

ObfuscationAttribute()

初始化 ObfuscationAttribute 类的新实例。Initializes a new instance of the ObfuscationAttribute class.

属性

ApplyToMembers

获取或设置一个 Boolean 值,该值指示某一类型的特性是否应用到该类型的成员。Gets or sets a Boolean value indicating whether the attribute of a type is to apply to the members of the type.

Exclude

获取或设置一个 Boolean 值,该值指示模糊处理工具是否应将类型或成员从模糊处理中排除。Gets or sets a Boolean value indicating whether the obfuscation tool should exclude the type or member from obfuscation.

Feature

获取或设置一个字符串值,该字符串值可由模糊处理工具识别并指定处理选项。Gets or sets a string value that is recognized by the obfuscation tool, and which specifies processing options.

StripAfterObfuscation

获取或设置一个 Boolean 值,该值指示模糊处理工具是否应在处理后移除此特性。Gets or sets a Boolean value indicating whether the obfuscation tool should remove this attribute after processing.

TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(继承自 Attribute)

方法

Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object.

(继承自 Attribute)
GetHashCode()

返回此实例的哈希代码。Returns the hash code for this instance.

(继承自 Attribute)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(继承自 Attribute)
Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(继承自 Attribute)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

显式界面实现

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface.

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(继承自 Attribute)

适用于

另请参阅