次の方法で共有


MethodBase.Attributes プロパティ

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

Public MustOverride ReadOnly Property Attributes As _
   MethodAttributes
[C#]
public abstract MethodAttributes Attributes {get;}
[C++]
public: __property virtual MethodAttributes get_Attributes() = 0;
[JScript]
public abstract function get Attributes() : MethodAttributes;

プロパティ値

MethodAttributes 値の 1 つ。

解説

すべてのメンバが属性のセットを持っています。属性は、メンバの特定の型との関係で定義されます。

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

実装時の注意: Attributes プロパティを使用して、メソッドが public か、 private か、 final か、仮想 (Visual Basic では Overridable) かなどを判断します。

使用例

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

 
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

Class AttributesSample

    Public Sub Mymethod(ByVal int1m As Integer, ByRef str2m As String, ByRef str3m As String)
        str2m = "in Mymethod"
    End Sub 'Mymethod

    Public Shared Function Main(ByVal args() As String) As Integer
        Console.WriteLine("Reflection.MethodBase.Attributes Sample")

        ' Get the type.
        Dim MyType As Type = Type.GetType("AttributesSample")

        ' Get the method Mymethod on the type.
        Dim Mymethodbase As MethodBase = MyType.GetMethod("Mymethod")

        ' Display the method name.
        Console.WriteLine("Mymethodbase = {0}.", Mymethodbase)

        ' Get the MethodAttribute enumerated value.
        Dim Myattributes As MethodAttributes = Mymethodbase.Attributes

        ' Display the flags that are set.
        PrintAttributes(GetType(System.Reflection.MethodAttributes), CInt(Myattributes))
        Return 0
    End Function 'Main

    Public Shared Sub PrintAttributes(ByVal attribType As Type, ByVal iAttribValue As Integer)
        If Not attribType.IsEnum Then
            Console.WriteLine("This type is not an enum.")
            Return
        End If
        Dim fields As FieldInfo() = attribType.GetFields((BindingFlags.Public Or BindingFlags.Static))
        Dim i As Integer
        For i = 0 To fields.Length - 1
            Dim fieldvalue As Integer = CType(fields(i).GetValue(Nothing), Int32)
            If (fieldvalue And iAttribValue) = fieldvalue Then
                Console.WriteLine(fields(i).Name)
            End If
        Next i
    End Sub 'PrintAttributes
End Class 'AttributesSample

[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 = (Int32)fields[i].GetValue(null);
            if ((fieldvalue & iAttribValue) == fieldvalue)
            {
                Console.WriteLine(fields[i].Name);
            }
        }
    }
}

[C++] 

#using <mscorlib.dll>
using namespace System;
using namespace System::Reflection;
using namespace System::Runtime::InteropServices;

public __gc class AttributesSample
{
public:
    void Mymethod (int int1m, [Out] String** str2m, String** str3m)
    {
        *str2m = S"in Mymethod";
    }
};

void PrintAttributes(Type* attribType, int iAttribValue)
{
    if (!attribType->IsEnum)
    { 
        Console::WriteLine(S"This type is not an enum."); 
        return; 
    }

    FieldInfo* fields[] =
        attribType->GetFields(static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Static));
    for (int i = 0; i < fields->Length; i++)
    {
        int fieldvalue = *dynamic_cast<Int32*>(fields[i]->GetValue(0));
        if ((fieldvalue & iAttribValue) == fieldvalue)
        {
            Console::WriteLine(fields[i]->Name);
        }
    }
}

int main()
{      
    Console::WriteLine (S"Reflection.MethodBase.Attributes Sample");

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

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

    // Display the method name.
    Console::WriteLine(S"Mymethodbase = {0}", 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;
}

[JScript] 

 import System;
 import System.Reflection;
 
 class AttributesSample
 {
    public function Mymethod (int1m : int) : void
    {
    }
 
    public static function Main() : void
    {      
       Console.WriteLine ("Reflection.MethodBase.Attributes Sample");
       
       // Get our type
       var MyType : Type = Type.GetType("AttributesSample");
 
       // Get the method Mymethod on our type
       var Mymethodbase : MethodBase = MyType.GetMethod("Mymethod");
 
       // Print out the method
       Console.WriteLine("Mymethodbase = " + Mymethodbase);
 
       // Get the MethodAttribute enumerated value
       var Myattributes : MethodAttributes = Mymethodbase.Attributes;
 
       // print out the flags set
       PrintAttributes( System.Reflection.MethodAttributes, int(Myattributes) );
    }
 
 
    public static function PrintAttributes( attribType : Type, iAttribValue : int ) : void 
    {
       if ( ! attribType.IsEnum ) { Console.WriteLine( "This type is not an enum" ); return; }
 
       var fields : FieldInfo[] = attribType.GetFields(BindingFlags.Public | BindingFlags.Static);
       for ( var i:int = 0; i < fields.Length; i++ )
       {
          var fieldvalue : int = int(fields[i].GetValue(null));
          if ( (fieldvalue & iAttribValue) == fieldvalue )
          {
             Console.WriteLine( "\t" + fields[i].Name );
          }
       }
    }
 }
 AttributesSample.Main();
/* 
 This code produces the following output:
 
Reflection.MethodBase.Attributes Sample
Mymethodbase = Void Mymethod(Int32)
        PrivateScope
        FamANDAssem
        Family
        Public
        Virtual
        HideBySig
        VtableLayoutMask
        ReuseSlot
        NewSlot
*/

このコードによって、次の出力が生成されます。

Reflection.MethodBase.Attributes Sample

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

PrivateScope

FamANDAssem

Family

Public

HideBySig

ReuseSlot

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

MethodBase クラス | MethodBase メンバ | System.Reflection 名前空間 | MethodAttributes