Type.Attributes 속성


Type과 관련된 특성을 가져옵니다.Gets the attributes associated with the Type.

 property System::Reflection::TypeAttributes Attributes { System::Reflection::TypeAttributes get(); };
public System.Reflection.TypeAttributes Attributes { get; }
member this.Attributes : System.Reflection.TypeAttributes
Public ReadOnly Property Attributes As TypeAttributes

속성 값

TypeAttributes이 값이 지정되어 있지 않은 제네릭 형식 매개 변수를 나타내지 않는 경우 Type의 특성 집합을 나타내는 Type 개체입니다.A TypeAttributes object representing the attribute set of the Type, unless the Type represents a generic type parameter, in which case the value is unspecified.



다음 예제에서는 Attributes 속성입니다.The following example uses the 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");
               case TypeAttributes.Public:
                   Console.WriteLine("   ...is public");
               case TypeAttributes.NestedPublic:
                   Console.WriteLine("   ...is nested and public");
               case TypeAttributes.NestedPrivate:
                   Console.WriteLine("   ...is nested and private");
               case TypeAttributes.NestedFamANDAssem:
                   Console.WriteLine("   ...is nested, and inheritable only within the assembly" +
                      "\n         (cannot be declared in C#)");
               case TypeAttributes.NestedAssembly:
                   Console.WriteLine("   ...is nested and internal");
               case TypeAttributes.NestedFamily:
                   Console.WriteLine("   ...is nested and protected");
               case TypeAttributes.NestedFamORAssem:
                   Console.WriteLine("   ...is nested and protected internal");

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

           // 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");
                       Console.WriteLine("   ...is a class");
               case TypeAttributes.Interface:
                   Console.WriteLine("   ...is an interface");

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

           if ((attr & TypeAttributes.Sealed) != 0)
               Console.WriteLine("   ...is sealed");
// The example displays the following output:
// Attributes for type Example:
//    ...is public
//    ...is AutoLayout
//    ...is a class
//    ...is abstract

// Attributes for type NestedClass:
//    ...is nested and protected
//    ...is AutoLayout
//    ...is a class
//    ...is sealed

// Attributes for type INested:
//    ...is nested and public
//    ...is AutoLayout
//    ...is an interface
//    ...is abstract

// Attributes for type S:
//    ...is not public
//    ...is SequentialLayout
//    ...is a value type
//    ...is sealed
Imports System.Reflection

Friend Structure S
    Public X As Integer
End Structure

Public MustInherit Class Example
    Protected NotInheritable Class NestedClass
    End Class

    Public Interface INested
    End Interface

    Public Shared Sub Main()
        ' Create an array of types.
        Dim types() As Type = { GetType(Example), GetType(NestedClass),
                                GetType(INested), GetType(S) }

        For Each t In types
           Console.WriteLine("Attributes for type {0}:", t.Name)

           Dim attr As TypeAttributes = t.Attributes

           ' Use the visibility mask to test for visibility attributes.
           Dim visibility As TypeAttributes = attr And TypeAttributes.VisibilityMask
           Select Case visibility
               Case TypeAttributes.NotPublic:
                   Console.WriteLine("   ...is not Public")
               Case TypeAttributes.Public:
                   Console.WriteLine("   ...is Public")
               Case TypeAttributes.NestedPublic:
                   Console.WriteLine("   ...is nested and Public")
               Case TypeAttributes.NestedPrivate:
                   Console.WriteLine("   ...is nested and Private")
               Case TypeAttributes.NestedFamANDAssem:
                   Console.WriteLine("   ...is nested, and inheritable only within the assembly" & _
                      vbLf & "         (cannot be declared in Visual Basic)")
               Case TypeAttributes.NestedAssembly:
                   Console.WriteLine("   ...is nested and Friend")
               Case TypeAttributes.NestedFamily:
                   Console.WriteLine("   ...is nested and Protected")
               Case TypeAttributes.NestedFamORAssem:
                   Console.WriteLine("   ...is nested and Protected Friend")
           End Select

           ' Use the layout mask to test for layout attributes.
           Dim layout As TypeAttributes = attr And TypeAttributes.LayoutMask
           Select Case layout
               Case TypeAttributes.AutoLayout:
                   Console.WriteLine("   ...is AutoLayout")
               Case TypeAttributes.SequentialLayout:
                   Console.WriteLine("   ...is SequentialLayout")
               Case TypeAttributes.ExplicitLayout:
                   Console.WriteLine("   ...is ExplicitLayout")
           End Select

           ' Use the class semantics mask to test for class semantics attributes.
           Dim classSemantics As TypeAttributes = attr And TypeAttributes.ClassSemanticsMask
           Select Case classSemantics
               Case TypeAttributes.Class:
                   If t.IsValueType Then
                       Console.WriteLine("   ...is a value type")
                       Console.WriteLine("   ...is a class")
                   End If
               Case TypeAttributes.Interface:
                   Console.WriteLine("   ...is an interface")
           End Select

           If 0 <> (attr And TypeAttributes.Abstract) Then _
               Console.WriteLine("   ...is MustInherit")

           If 0 <> (attr And TypeAttributes.Sealed) Then _
               Console.WriteLine("   ...is NotInheritable")
    End Sub
End Class
' 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


일부 멤버는 TypeAttributes 열거형 값의 그룹을 나타내는 마스크 됩니다.Some members of the TypeAttributes enumeration are masks that represent a group of values. 각 그룹에는 기본 값이 0 인 멤버로 포함 됩니다.Each group includes one member whose underlying value is zero. 예를 들어, 기본 값 합니다 TypeAttributes.NotPublic 멤버를 TypeAttributes.VisibilityMask 그룹은 0을 그대로 TypeAttributes.AutoLayout 멤버를 TypeAttributes.SequentialLayout 그룹.For example, the underlying value of the TypeAttributes.NotPublic member in the TypeAttributes.VisibilityMask group is zero, as is the TypeAttributes.AutoLayout member in the TypeAttributes.SequentialLayout group. 이 때문에 이러한 값을 테스트 하기 전에 마스크를 사용 해야 합니다.Because of this, you must use the mask before testing for those values. 예제에서는 그림을 제공합니다.The example provides an illustration.

대부분의 경우 속성 같은 IsClass,IsAutoLayout, 및 IsSpecialName 유형 특성 보다 사용 하기가 더 쉽습니다.For most purposes, properties like IsClass,IsAutoLayout, and IsSpecialName are easier to use than type attributes.

하는 경우 현재 Type 나타내는 생성된 된 제네릭 형식,이 속성에는 제네릭 형식 정의의 특성을 반환 합니다.If the current Type represents a constructed generic type, this property returns the attributes of the generic type definition. 예를 들어,에 대 한 반환 하는 특성 MyGenericClass<int> (MyGenericClass(Of Integer) Visual Basic의) 특성 MyGenericClass<T> (MyGenericClass(Of T) Visual basic에서).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).

현재 Type가 제네릭 형식 매개 변수를 나타내면이 고, IsGenericParameter 속성이 true을 반환 하면이 속성이 반환 하는 TypeAttributes 값이 지정 되지 않습니다.If the current Type represents a generic type parameter - that is, if the IsGenericParameter property returns true - the TypeAttributes value returned by this property is unspecified.

적용 대상

추가 정보