英語で読む

次の方法で共有


MethodBase.Attributes プロパティ

定義

このメソッドに関連付けられている属性を取得します。

C#
public abstract System.Reflection.MethodAttributes Attributes { get; }

プロパティ値

MethodAttributes 値のいずれか 1 つ。

実装

次のコード例では、ユーザー定義メソッド Mymethod の属性を表示します。

C#

using System;
using System.Reflection;

class AttributesSample
{
    public void Mymethod (int int1m, out string str2m, ref string str3m)
    {
        str2m = "in Mymethod";
    }

    public static int Main(string[] args)
    {
        Console.WriteLine ("Reflection.MethodBase.Attributes Sample");

        // Get the type.
        Type MyType = Type.GetType("AttributesSample");

        // Get the method Mymethod on the type.
        MethodBase Mymethodbase = MyType.GetMethod("Mymethod");

        // Display the method name.
        Console.WriteLine("Mymethodbase = " + Mymethodbase);

        // Get the MethodAttribute enumerated value.
        MethodAttributes Myattributes = Mymethodbase.Attributes;

        // Display the flags that are set.
        PrintAttributes(typeof(System.Reflection.MethodAttributes), (int) Myattributes);
        return 0;
    }

    public static void PrintAttributes(Type attribType, int iAttribValue)
    {
        if (!attribType.IsEnum)
        {
            Console.WriteLine("This type is not an enum.");
            return;
        }

        FieldInfo[] fields = attribType.GetFields(BindingFlags.Public | BindingFlags.Static);
        for (int i = 0; i < fields.Length; i++)
        {
            int fieldvalue = (int)fields[i].GetValue(null);
            if ((fieldvalue & iAttribValue) == fieldvalue)
            {
                Console.WriteLine(fields[i].Name);
            }
        }
    }
}

このコードを実行すると、次の出力が生成されます。

Reflection.MethodBase.Attributes サンプル

Mymethodbase = Void Mymethod(Int32, System.String ByRef, System.String ByRef)

PrivateScope

FamANDAssem

Family

パブリック

HideBySig

ReuseSlot

注釈

すべてのメンバーには、特定の種類のメンバーに関連して定義される属性のセットがあります。

を取得するには、最初に MethodAttributes型を取得します。 型から メソッドを取得します。 メソッドから を取得します MethodAttributes

注意 (実装者)

メソッドvirtualfinalprivatepublicAttributes 、などであるかどうかを判断するには、 プロパティを使用します。

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.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 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

こちらもご覧ください