MethodBase.Attributes 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得與這個方法相關的屬性 (Attribute)。
public:
abstract property System::Reflection::MethodAttributes Attributes { System::Reflection::MethodAttributes get(); };
public abstract System.Reflection.MethodAttributes Attributes { get; }
member this.Attributes : System.Reflection.MethodAttributes
Public MustOverride ReadOnly Property Attributes As MethodAttributes
屬性值
其中一個 MethodAttributes 值。
實作
範例
下列程式代碼範例會顯示使用者定義方法 Mymethod 的屬性。
using namespace System;
using namespace System::Reflection;
using namespace System::Runtime::InteropServices;
public ref class AttributesSample
{
public:
void Mymethod( int int1m, [Out]interior_ptr<String^> str2m, interior_ptr<String^> str3m )
{
*str2m = "in Mymethod";
}
};
void PrintAttributes( Type^ attribType, int iAttribValue )
{
if ( !attribType->IsEnum )
{
Console::WriteLine( "This type is not an enum." );
return;
}
array<FieldInfo^>^fields = attribType->GetFields( static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Static) );
for ( int i = 0; i < fields->Length; i++ )
{
int fieldvalue = safe_cast<Int32>(fields[ i ]->GetValue( nullptr ));
if ( (fieldvalue & iAttribValue) == fieldvalue )
{
Console::WriteLine( fields[ i ]->Name );
}
}
}
int main()
{
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 = {0}", Mymethodbase );
// Get the MethodAttribute enumerated value.
MethodAttributes Myattributes = Mymethodbase->Attributes;
// Display the flags that are set.
PrintAttributes( System::Reflection::MethodAttributes::typeid, (int)Myattributes );
return 0;
}
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);
}
}
}
}
Imports System.Reflection
Class AttributesSample
Public Sub Mymethod(ByVal int1m As Integer, ByRef str2m As String, ByRef str3m As String)
str2m = "in Mymethod"
End Sub
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
End Class
此程式碼會產生下列輸出:
Reflection.MethodBase.Attributes 範例
Mymethodbase = Void Mymethod (Int32、System.String ByRef、System.String ByRef)
PrivateScope
FamANDAssem
系列
公用
HideBySig
ReuseSlot
備註
所有成員都有一組屬性,這些屬性定義於特定成員類型。
若要取得 MethodAttributes,請先取得類型。 從類型取得 方法。 從方法取得 MethodAttributes。
給實施者的注意事項
Attributes使用 屬性來判斷方法是否為 public
、private
、final
、 virtual
等等。