英語で読む

次の方法で共有


Module.GetCustomAttributes メソッド

定義

カスタム属性を返します。

オーバーロード

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