Export (0) Print
Expand All
Expand Minimize

Type.Attributes Property

Gets the attributes associated with the Type.

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

public TypeAttributes Attributes { get; }

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.

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

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