Export (0) Print
Expand All
Expand Minimize

Type.Attributes Property

Gets the attributes associated with the Type.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public ReadOnly Property Attributes As TypeAttributes

Property Value

Type: System.Reflection.TypeAttributes
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.

Implements

_Type.Attributes

Some members of the 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 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.

TipTip

For most purposes, properties like IsClass, IsAutoLayout, and IsSpecialName are easier to use than type attributes.

If the current 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 Type represents a generic type parameter — that is, if the IsGenericParameter property returns true — the TypeAttributes value returned by this property is unspecified.

The following example uses the Attributes property.

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")
                   Else
                       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")
           Console.WriteLine()
       Next 
    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

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft