Module.IsDefined 方法

确定是否在此模块上定义了指定的 attributeType。

**命名空间:**System.Reflection
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Overridable Function IsDefined ( _
    attributeType As Type, _
    inherit As Boolean _
) As Boolean
用法
Dim instance As Module
Dim attributeType As Type
Dim inherit As Boolean
Dim returnValue As Boolean

returnValue = instance.IsDefined(attributeType, inherit)
public virtual bool IsDefined (
    Type attributeType,
    bool inherit
)
public:
virtual bool IsDefined (
    Type^ attributeType, 
    bool inherit
)
public boolean IsDefined (
    Type attributeType, 
    boolean inherit
)
public function IsDefined (
    attributeType : Type, 
    inherit : boolean
) : boolean

参数

  • attributeType
    自定义属性应用于的 Type 对象。
  • inherit
    对于该类型的对象,该参数被忽略。

返回值

如果此模块上定义了一个或多个 attributeType 实例,则为 true;否则为 false

异常

异常类型 条件

ArgumentNullException

attributeType 为 空引用(在 Visual Basic 中为 Nothing)。

示例

下面的示例说明如何使用 IsDefined 方法。

Imports System
Imports System.Reflection

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

'Define a module-level attribute.
Namespace ReflectionModule_Examples
    Class MyMainClass
        Shared Sub Main()
            Dim moduleArray() As [Module]

            moduleArray = [Assembly].GetExecutingAssembly().GetModules(False)

            'In a simple project with only one module, the module at index
            ' 0 will be the module containing these classes.
            Dim myModule As [Module] = moduleArray(0)

            Dim myType As Type
            myType = myModule.GetType("ReflectionModule_Examples.MySimpleAttribute")
            Console.WriteLine("IsDefined(MySimpleAttribute) = {0}", myModule.IsDefined(myType, False))
        End Sub 'Main
    End Class 'MyMainClass

    '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 'New
    End Class 'MySimpleAttribute
End Namespace 'ReflectionModule_Examples
using System;
using System.Reflection;

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

namespace ReflectionModule_Examples
{
    class MyMainClass
    {
        static void Main()
        {
            Module[] moduleArray;
            
            moduleArray = Assembly.GetExecutingAssembly().GetModules(false);
            
            //In a simple project with only one module, the module at index
            // 0 will be the module containing these classes.
            Module myModule = moduleArray[0];

            Type myType;
            myType = myModule.GetType("ReflectionModule_Examples.MySimpleAttribute");
            Console.WriteLine("IsDefined(MySimpleAttribute) = {0}", myModule.IsDefined(myType, false));
        }
    }

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

        public MySimpleAttribute(string newName)
        {
            name = newName;
        }
    }
}
using namespace System;
using namespace System::Reflection;

namespace ReflectionModule_Examples
{

   //A very simple custom attribute.

   [AttributeUsage(AttributeTargets::Class|AttributeTargets::Module)]
   public ref class MySimpleAttribute: public Attribute
   {
   private:
      String^ name;

   public:
      MySimpleAttribute( String^ newName )
      {
         name = newName;
      }

   };

}


//Define a module-level attribute.

[module:ReflectionModule_Examples::MySimpleAttribute("module-level")];
int main()
{
   array<System::Reflection::Module^>^moduleArray;
   moduleArray = Assembly::GetExecutingAssembly()->GetModules( false );
   
   //In a simple project with only one module, the module at index
   // 0 will be the module containing these classes.
   System::Reflection::Module^ myModule = moduleArray[ 0 ];
   Type^ myType;
   myType = myModule->GetType( "ReflectionModule_Examples.MySimpleAttribute" );
   Console::WriteLine( "IsDefined(MySimpleAttribute) = {0}", myModule->IsDefined( myType, false ) );
}
package ReflectionModule_Examples;

import System.*;
import System.Reflection.*;

//Define a module-level attribute.
/** @module ReflectionModule_Examples.MySimpleAttribute("module-level")
 */
class MyMainClass
{
    public static void main(String[] args)
    {
        Module moduleArray[];

        moduleArray = Assembly.GetExecutingAssembly().GetModules(false);
        //In a simple project with only one module, the module at index
        // 0 will be the module containing these classes.
        Module myModule = (Module)moduleArray.get_Item(0);

        Type myType;
        myType = myModule.GetType("ReflectionModule_Examples.MySimpleAttribute");
        Console.WriteLine("IsDefined(MySimpleAttribute) = {0}", 
            System.Convert.ToString(myModule.IsDefined(myType, false)));
    } //main
} //MyMainClass

//A very simple custom attribute.
/** @attribute AttributeUsage(AttributeTargets.Class | AttributeTargets.Module)
 */
public class MySimpleAttribute extends Attribute
{
    private String name;

    public MySimpleAttribute(String newName)
    {
        name = newName;
    } //MySimpleAttribute
} //MySimpleAttribute

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

请参见

参考

Module 类
Module 成员
System.Reflection 命名空间