Module.GetCustomAttributes 方法

定義

傳回自訂屬性 (Attribute)。

多載

GetCustomAttributes(Boolean)

傳回所有自訂屬性。

GetCustomAttributes(Type, Boolean)

取得指定型別的自訂屬性。

GetCustomAttributes(Boolean)

來源:
Module.cs
來源:
Module.cs
來源:
Module.cs

傳回所有自訂屬性。

C#
public virtual object[] GetCustomAttributes (bool inherit);

參數

inherit
Boolean

這個類型的物件會忽略這個引數。

傳回

Object[]

Object 型別的陣列,包含所有自訂屬性。

實作

範例

下列範例會顯示符合指定搜尋準則的模組名稱。

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];
            object[] attributes;
            attributes = myModule.GetCustomAttributes(true);
            foreach(Object o in attributes)
            {
                Console.WriteLine("Found this attribute on myModule: {0}.", o.ToString());
            }
        }
    }
    //A very simple custom attribute.
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Module)]
    public class MySimpleAttribute : Attribute
    {
        private string name;

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

適用於

.NET 9 和其他版本
產品 版本
.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

GetCustomAttributes(Type, Boolean)

來源:
Module.cs
來源:
Module.cs
來源:
Module.cs

取得指定型別的自訂屬性。

C#
public virtual object[] GetCustomAttributes (Type attributeType, bool inherit);

參數

attributeType
Type

要取得的屬性型別。

inherit
Boolean

這個類型的物件會忽略這個引數。

傳回

Object[]

Object 型別的陣列,包含指定型別的所有自訂屬性。

實作

例外狀況

attributeTypenull

attributeType 不是執行階段所提供的 Type 物件。 例如,attributeTypeTypeBuilder 物件。

範例

下列範例會顯示符合指定搜尋準則之指定型別的模組名稱。

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];
            object[] attributes;
            //Get only MySimpleAttribute attributes for this module.
            attributes = myModule.GetCustomAttributes(
                myModule.GetType("ReflectionModule_Examples.MySimpleAttribute", false, false),
                true);
            foreach(Object o in attributes)
            {
                Console.WriteLine("Found this attribute on myModule: {0}", o.ToString());
            }
        }
    }

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

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

適用於

.NET 9 和其他版本
產品 版本
.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