Assembly.IsDefined(Type, Boolean) 方法

定义

指示指定的属性是否已应用于该程序集。

public:
 virtual bool IsDefined(Type ^ attributeType, bool inherit);
public virtual bool IsDefined (Type attributeType, bool inherit);
abstract member IsDefined : Type * bool -> bool
override this.IsDefined : Type * bool -> bool
Public Overridable Function IsDefined (attributeType As Type, inherit As Boolean) As Boolean

参数

attributeType
Type

要为此程序集检查的属性类型。

inherit
Boolean

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

返回

如果已将该属性应用于程序集,则为 true;否则为 false

实现

例外

attributeTypenull

attributeType 使用的类型无效。

示例

下面的代码示例将 AssemblyTitleAttribute 特性应用于程序集,然后使用 IsDefined 指示是否已应用该特性。 它还测试未应用的属性。

using System;
using System.Reflection;

// Set an assembly attribute.
[assembly:AssemblyTitleAttribute("A title example")]

// Note that the suffix "Attribute" can be omitted:
// [assembly:AssemblyTitle("A title example")]

public class Test {

    public static void Main() {

        // Get the assembly that is executing this method.
        Assembly asm = Assembly.GetCallingAssembly();

        // Get the attribute type just defined.
        Type aType = typeof(AssemblyTitleAttribute);
        Console.WriteLine(asm.IsDefined(aType, false));

        // Try an attribute not defined.
        aType = typeof(AssemblyVersionAttribute);
        Console.WriteLine(asm.IsDefined(aType, false));
    }
}
//
//  This code example produces the following output:
//  True
//  False
//

Imports System.Reflection

' Set an assembly attribute.
<Assembly:AssemblyTitleAttribute("A title example")>

' Note that the suffix "Attribute" can be omitted:
' <Assembly:AssemblyTitle("A title examle")>

Public Class Test
    Public Shared Sub Main()

        ' Get the assembly that is executing this method.
        Dim asm As [Assembly] = [Assembly].GetCallingAssembly

        ' Get the attribute type just defined.
        Dim aType As Type = GetType(AssemblyTitleAttribute)

        Console.WriteLine(asm.IsDefined(aType, false))

        ' Try an attribute not defined.
        aType = GetType(AssemblyVersionAttribute)

        Console.WriteLine(asm.IsDefined(aType, false))

    End Sub
End Class

' This code example produces the following output:
' True
' False
'

适用于