Cet article a fait l’objet d’une traduction automatique. Pour afficher l’article en anglais, activez la case d’option Anglais. Vous pouvez également afficher le texte anglais dans une fenêtre contextuelle en faisant glisser le pointeur de la souris sur le texte traduit.
Traduction
Anglais

Type.Attributes propriété

 

Date de publication : novembre 2016

Obtient les attributs associés à Type.

Espace de noms:   System
Assembly:  mscorlib (dans mscorlib.dll)

public TypeAttributes Attributes { get; }

Valeur de propriété

Type: System.Reflection.TypeAttributes

Objet TypeAttributes qui représente l'ensemble d'attributs du Type, à moins que le Type représente un paramètre de type générique, cas dans lequel la valeur est non spécifiée.

Implémente

_Type.Attributes

Some members of the T:System.Reflection.TypeAttributes enumeration are masks that represent a group of values. Each group includes one member whose underlying value is zero. For example, the underlying value of the F:System.Reflection.TypeAttributes.NotPublic member in the F:System.Reflection.TypeAttributes.VisibilityMask group is zero, as is the F:System.Reflection.TypeAttributes.AutoLayout member in the F:System.Reflection.TypeAttributes.SequentialLayout group. Because of this, you must use the mask before testing for those values. The example provides an illustration.

System_CAPS_tipAstuce

For most purposes, properties likeP:System.Type.IsClass,P:System.Type.IsAutoLayout, andP:System.Type.IsSpecialName are easier to use than type attributes.

If the current T:System.Type represents a constructed generic type, this property returns the attributes of the generic type definition. For example, the attributes returned for MyGenericClass<int> (MyGenericClass(Of Integer) in Visual Basic) are the attributes of MyGenericClass<T> (MyGenericClass(Of T) in Visual Basic).

If the current T:System.Type represents a generic type parameter — that is, if the P:System.Type.IsGenericParameter property returns true — the T:System.Reflection.TypeAttributes value returned by this property is unspecified.

The following example usesthe P:System.Type.Attributes property.

using System;
using System.Reflection;

internal struct S
{
    public int X;
}

public abstract class Example
{
    protected sealed class NestedClass {}

    public interface INested {}

    public static void Main()
    {
        // Create an array of types.
        Type[] types = { typeof(Example), typeof(NestedClass),
                         typeof(INested), typeof(S) };

        foreach (var t in types) {
           Console.WriteLine("Attributes for type {0}:", t.Name);

           TypeAttributes attr = t.Attributes;

           // To test for visibility attributes, you must use the visibility mask.
           TypeAttributes visibility = attr & TypeAttributes.VisibilityMask;
           switch (visibility)
           {
               case TypeAttributes.NotPublic:
                   Console.WriteLine("   ...is not public");
                   break;
               case TypeAttributes.Public:
                   Console.WriteLine("   ...is public");
                   break;
               case TypeAttributes.NestedPublic:
                   Console.WriteLine("   ...is nested and public");
                   break;
               case TypeAttributes.NestedPrivate:
                   Console.WriteLine("   ...is nested and private");
                   break;
               case TypeAttributes.NestedFamANDAssem:
                   Console.WriteLine("   ...is nested, and inheritable only within the assembly" +
                      "\n         (cannot be declared in C#)");
                   break;
               case TypeAttributes.NestedAssembly:
                   Console.WriteLine("   ...is nested and internal");
                   break;
               case TypeAttributes.NestedFamily:
                   Console.WriteLine("   ...is nested and protected");
                   break;
               case TypeAttributes.NestedFamORAssem:
                   Console.WriteLine("   ...is nested and protected internal");
                   break;
           }

           ' Use the layout mask to test for layout attributes.
           TypeAttributes layout = attr & TypeAttributes.LayoutMask;
           switch (layout)
           {
               case TypeAttributes.AutoLayout:
                   Console.WriteLine("   ...is AutoLayout");
                   break;
               case TypeAttributes.SequentialLayout:
                   Console.WriteLine("   ...is SequentialLayout");
                   break;
               case TypeAttributes.ExplicitLayout:
                   Console.WriteLine("   ...is ExplicitLayout");
                   break;
           }

           ' Use the class semantics mask to test for class semantics attributes.
           TypeAttributes classSemantics = attr & TypeAttributes.ClassSemanticsMask;
           switch (classSemantics)
           {
               case TypeAttributes.Class:
                   if (t.IsValueType)
                   {
                       Console.WriteLine("   ...is a value type");
                   }
                   else
                   {
                       Console.WriteLine("   ...is a class");
                   }
                   break;
               case TypeAttributes.Interface:
                   Console.WriteLine("   ...is an interface");
                   break;
           }

           if (0!=(attr & TypeAttributes.Abstract))
           {
               Console.WriteLine("   ...is abstract");
           }

           if (0!=(attr & TypeAttributes.Sealed))
           {
               Console.WriteLine("   ...is sealed");
           }
           Console.WriteLine();
       }
    }
}
// The example displays the following output:
//       Attributes for type Example:
//          ...is Public
//          ...is AutoLayout
//          ...is a class
//          ...is MustInherit
//
//       Attributes for type NestedClass:
//          ...is nested and Protected
//          ...is AutoLayout
//          ...is a class
//          ...is NotInheritable
//
//       Attributes for type INested:
//          ...is nested and Public
//          ...is AutoLayout
//          ...is an interface
//          ...is MustInherit
//
//       Attributes for type S:
//          ...is not Public
//          ...is SequentialLayout
//          ...is a value type
//          ...is NotInheritable

.NET Framework
Disponible depuis 1.1
Bibliothèque de classes portable
Pris en charge dans : plateformes .NET portables
Silverlight
Disponible depuis 2.0
Silverlight pour Windows Phone
Disponible depuis 7.0
Retour au début
Afficher: