Assembly.IsDefined(Type, Boolean) Assembly.IsDefined(Type, Boolean) Assembly.IsDefined(Type, Boolean) Assembly.IsDefined(Type, Boolean) Method

定义

指示指定的属性是否已应用于该程序集。Indicates whether or not a specified attribute has been applied to the assembly.

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 Type Type Type

要为此程序集检查的属性类型。The type of the attribute to be checked for this assembly.

inherit
Boolean Boolean Boolean Boolean

对于该类型的对象,将忽略此参数。This argument is ignored for objects of this type.

返回

如果已将该属性应用于程序集,则为 true;否则为 falsetrue if the attribute has been applied to the assembly; otherwise, false.

实现

异常

attributeType 使用的类型无效。attributeType uses an invalid type.

示例

下面的代码示例适用AssemblyTitleAttribute属性设置为程序集,然后使用IsDefined以指示是否已应用。The following code example applies the AssemblyTitleAttribute attribute to an assembly and then uses IsDefined to indicate whether it was applied. 该语句还测试未应用的属性。It also tests an attribute that was not applied.

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
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
'

适用于