Module.IsDefined(Type, Boolean) 方法

定义

返回一个值,该值指示是否已将指定的特性类型应用于此模块。

C#
public virtual bool IsDefined (Type attributeType, bool inherit);

参数

attributeType
Type

要测试的自定义属性的类型。

inherit
Boolean

对于该类型的对象,将忽略此自变量。

返回

如果一个或多个 attributeType 实例已应用于此模块,则为 true;否则为 false

实现

例外

attributeTypenull

attributeType 不是由运行时提供的 Type 对象。 例如,attributeType 是一个 TypeBuilder 对象。

示例

以下示例演示如何使用 IsDefined 方法。

C#
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 = typeof(MyMainClass).Assembly.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;
        }
    }
}

适用于

产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1