共用方式為


Module.IsDefined Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Determines if the specified attribute type is applied to this module.

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

Syntax

'Declaration
Public Overridable Function IsDefined ( _
    attributeType As Type, _
    inherit As Boolean _
) As Boolean
public virtual bool IsDefined(
    Type attributeType,
    bool inherit
)

Parameters

  • attributeType
    Type: System.Type
    The type of custom attribute to search for.
  • inherit
    Type: System.Boolean
    This argument is ignored for objects of this type.

Return Value

Type: System.Boolean
true if one or more instances of attributeType are applied to this module; otherwise, false.

Implements

ICustomAttributeProvider.IsDefined(Type, Boolean)

Exceptions

Exception Condition
ArgumentNullException

attributeType is nulla null reference (Nothing in Visual Basic).

ArgumentException

attributeType is not a Type object supplied by the runtime. For example, attributeType is a TypeBuilder object.

Examples

The following example defines an attribute, applies it to the example's module , and uses the IsDefined method to show that the attribute was applied to the module.

Imports System.Reflection

' Define a module-level attribute.
<Module: MySimpleAttribute("module-level")> 

Class Example
    Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
        Dim moduleArray() As [Module] = _
            [Assembly].GetExecutingAssembly().GetModules()
        Dim myModule As [Module] = moduleArray(0)
        outputBlock.Text &= _
            String.Format("IsDefined(MySimpleAttribute) = {0}" & vbLf, _
            myModule.IsDefined(GetType(MySimpleAttribute), False))
    End Sub 
End Class 

'A very simple custom attribute.
<AttributeUsage(AttributeTargets.Class Or AttributeTargets.Module)> _
Public Class MySimpleAttribute
    Inherits Attribute
    Private name As String
    Public Sub New(ByVal newName As String)
        name = newName
    End Sub 
End Class 

' This example produces output similar to the following:
'
'IsDefined(MySimpleAttribute) = True
using System;
using System.Reflection;

//Define a module-level attribute.
[module: MySimpleAttribute("module-level")]

class Example
{
    public static void Demo(System.Windows.Controls.TextBlock outputBlock)
    {
        Module[] moduleArray = Assembly.GetExecutingAssembly().GetModules();
        Module myModule = moduleArray[0];
        outputBlock.Text += 
            String.Format("IsDefined(MySimpleAttribute) = {0}\n", 
                          myModule.IsDefined(typeof(MySimpleAttribute), false));
    }
}

//A very simple custom attribute.
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Module)]
public class MySimpleAttribute : Attribute
{
    private string name;

    public MySimpleAttribute(string newName)
    {
        name = newName;
    }
}

/* This example produces output similar to the following:

IsDefined(MySimpleAttribute) = True
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.