ObfuscationAttribute Klas

Definitie

Hiermee geeft u verdoofde hulpprogramma's de opdracht om de opgegeven acties voor een assembly, type of lid uit te voeren.

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)]
[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)]
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)>]
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)>]
type ObfuscationAttribute = class
    inherit Attribute
Public NotInheritable Class ObfuscationAttribute
Inherits Attribute
Overname
ObfuscationAttribute
Kenmerken

Voorbeelden

In het volgende codevoorbeeld ziet u een openbare assembly met twee typen: Type1 en Type2. De assembly is gemarkeerd voor verdoofing met de ObfuscateAssemblyAttribute, die de assembly markeert die moet worden behandeld als openbaar (dat wil gezegd, de AssemblyIsPrivate eigenschap is false).

Type1 is gemarkeerd voor verdoofing omdat de assembly is gemarkeerd voor verdoofing. Eén lid van is uitgesloten van Type1 verdoofing met behulp van de Exclude eigenschap.

Type2 is uitgesloten van verdoofing, maar de leden zijn gemarkeerd voor verdoofing omdat de ApplyToMembers eigenschap is false.

De MethodA methode Type2 wordt gemarkeerd met de waarde "default" voor de Feature eigenschap. Het is noodzakelijk om op te geven false voor de Exclude eigenschap om uit MethodA te sluiten van verdoofing, omdat de standaardwaarde voor de Exclude eigenschap is true. Het verdoofhulpmiddel mag het kenmerk niet verwijderen na verdoofing omdat de StripAfterObfuscation eigenschap is false. Alle andere kenmerken in dit codevoorbeeld worden verwijderd na verdoofing, omdat de StripAfterObfuscation eigenschap niet is opgegeven en daarom wordt standaard ingesteld trueop .

Het codevoorbeeld bevat code om de kenmerken en de bijbehorende eigenschappen weer te geven. U kunt ook de kenmerken onderzoeken door het DLL-bestand te openen met de 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

Opmerkingen

Met de ObfuscationAttribute kenmerken kunnen ObfuscateAssemblyAttribute assemblyauteurs aantekeningen toevoegen aan hun binaire bestanden, zodat verborgen hulpprogramma's deze correct kunnen verwerken met minimale externe configuratie.

Important

Als u dit kenmerk toepast, wordt de code-entiteit waarop u dit toepast niet automatisch verborgen. Het toepassen van het kenmerk is een alternatief voor het maken van een configuratiebestand voor het verborgen hulpprogramma. Dat wil gezegd, het biedt slechts instructies voor een verdoezelingsprogramma. Microsoft raadt aan dat leveranciers van verborgen hulpprogramma's de semantiek volgen die hier worden beschreven. Er is echter geen garantie dat een bepaald hulpprogramma Microsoft aanbevelingen volgt.

Het ObfuscationAttribute kenmerk heeft een tekenreekseigenschap Feature . Hulpprogramma's voor verdoofing kunnen de tekenreekswaarden van deze eigenschap toewijzen aan functies die ze implementeren, bij voorkeur met behulp van een XML-configuratiebestand waartoe gebruikers toegang hebben. Hiermee ObfuscationAttribute worden twee functietekenreeksen gedefinieerd: 'standaard' en 'alle'. De tekenreeks 'standaard' moet worden toegewezen aan de standaard verborgen functies van een hulpprogramma en 'all' moet worden toegewezen aan de volledige set verborgen functies die worden ondersteund door een hulpprogramma. De standaardwaarde van de Feature eigenschap is 'all', waardoor de volledige set verborgen functies wordt ingeschakeld.

Wanneer dit wordt toegepast op een assembly, ObfuscationAttribute geldt dit ook voor alle typen binnen de assembly. Als de ApplyToMembers eigenschap niet is opgegeven of is ingesteld trueop, is het kenmerk ook van toepassing op alle leden. ObfuscationAttribute geeft niet op of een assembly openbaar of privé is. Als u wilt opgeven of een assembly openbaar of privé is, gebruikt u het ObfuscateAssemblyAttribute kenmerk.

Wanneer deze wordt toegepast op klassen en structuren, ObfuscationAttribute geldt dit ook voor alle leden van het type als de ApplyToMembers eigenschap niet is opgegeven of is ingesteld trueop .

Wanneer dit wordt toegepast op methoden, parameters, velden en eigenschappen, is het kenmerk alleen van invloed op de entiteit waarop het wordt toegepast.

Constructors

Name Description
ObfuscationAttribute()

Initialiseert een nieuw exemplaar van de ObfuscationAttribute klasse.

Eigenschappen

Name Description
ApplyToMembers

Hiermee wordt een Boolean waarde opgehaald of ingesteld die aangeeft of het kenmerk van een type moet worden toegepast op de leden van het type.

Exclude

Hiermee wordt een Boolean waarde opgehaald of ingesteld die aangeeft of het hulpprogramma voor verdoofing het type of lid moet uitsluiten van verdoofing.

Feature

Hiermee wordt een tekenreekswaarde opgehaald of ingesteld die wordt herkend door het hulpprogramma voor verdoofing en waarmee verwerkingsopties worden opgegeven.

StripAfterObfuscation

Hiermee wordt een Boolean waarde opgehaald of ingesteld die aangeeft of het verdoofingsprogramma dit kenmerk na verwerking moet verwijderen.

TypeId

Wanneer deze wordt geïmplementeerd in een afgeleide klasse, krijgt u Attributehiervoor een unieke id.

(Overgenomen van Attribute)

Methoden

Name Description
Equals(Object)

Retourneert een waarde die aangeeft of dit exemplaar gelijk is aan een opgegeven object.

(Overgenomen van Attribute)
GetHashCode()

Retourneert de hash-code voor dit exemplaar.

(Overgenomen van Attribute)
GetType()

Hiermee haalt u de Type huidige instantie op.

(Overgenomen van Object)
IsDefaultAttribute()

Wanneer deze wordt overschreven in een afgeleide klasse, geeft u aan of de waarde van dit exemplaar de standaardwaarde is voor de afgeleide klasse.

(Overgenomen van Attribute)
Match(Object)

Wanneer deze wordt overschreven in een afgeleide klasse, wordt een waarde geretourneerd die aangeeft of dit exemplaar gelijk is aan een opgegeven object.

(Overgenomen van Attribute)
MemberwiseClone()

Hiermee maakt u een ondiepe kopie van de huidige Object.

(Overgenomen van Object)
ToString()

Retourneert een tekenreeks die het huidige object vertegenwoordigt.

(Overgenomen van Object)

Expliciete interface-implementaties

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

Hiermee wordt een set namen toegewezen aan een bijbehorende set verzend-id's.

(Overgenomen van Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Hiermee haalt u de typegegevens voor een object op, die kan worden gebruikt om de typegegevens voor een interface op te halen.

(Overgenomen van Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Hiermee wordt het aantal type-informatieinterfaces opgehaald dat een object biedt (0 of 1).

(Overgenomen van Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Biedt toegang tot eigenschappen en methoden die door een object worden weergegeven.

(Overgenomen van Attribute)

Van toepassing op

Zie ook