CallerMemberNameAttribute 類別

定義

可讓您取得方法呼叫端的方法或屬性名稱。

public ref class CallerMemberNameAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Parameter, Inherited=false)]
public sealed class CallerMemberNameAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Parameter, Inherited=false)>]
type CallerMemberNameAttribute = class
    inherit Attribute
Public NotInheritable Class CallerMemberNameAttribute
Inherits Attribute
繼承
CallerMemberNameAttribute
屬性

範例

下列範例會示範如何使用 CallerMemberName 屬性。 在方法的每個呼叫 TraceMessage 上,呼叫端資訊都會取代為選擇性參數的引數。

public void DoProcessing()
{
    TraceMessage("Something happened.");
}

public void TraceMessage(string message,
        [System.Runtime.CompilerServices.CallerMemberName] string memberName = "",
        [System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "",
        [System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0)
{
    System.Diagnostics.Trace.WriteLine("message: " + message);
    System.Diagnostics.Trace.WriteLine("member name: " + memberName);
    System.Diagnostics.Trace.WriteLine("source file path: " + sourceFilePath);
    System.Diagnostics.Trace.WriteLine("source line number: " + sourceLineNumber);
}

// Sample Output:
//  message: Something happened.
//  member name: DoProcessing
//  source file path: c:\Users\username\Documents\Visual Studio 2012\Projects\CallerInfoCS\CallerInfoCS\Form1.cs
//  source line number: 31
Private Sub DoProcessing()
    TraceMessage("Something happened.")
End Sub

Public Sub TraceMessage(message As String,
        <System.Runtime.CompilerServices.CallerMemberName> Optional memberName As String = Nothing,
        <System.Runtime.CompilerServices.CallerFilePath> Optional sourcefilePath As String = Nothing,
        <System.Runtime.CompilerServices.CallerLineNumber()> Optional sourceLineNumber As Integer = 0)

    System.Diagnostics.Trace.WriteLine("message: " & message)
    System.Diagnostics.Trace.WriteLine("member name: " & memberName)
    System.Diagnostics.Trace.WriteLine("source file path: " & sourcefilePath)
    System.Diagnostics.Trace.WriteLine("source line number: " & sourceLineNumber)
End Sub

' Sample output:
'   message: Something happened.
'   member name: DoProcessing
'   source file path: C:\Users\username\Documents\Visual Studio 2012\Projects\CallerInfoVB\CallerInfoVB\Form1.vb
'   source line number: 15

備註

您可以將 屬性套用 CallerMemberName 至具有預設值的選擇性參數。 您必須為選擇性參數指定明確的預設值。 您無法將此屬性套用至未指定為選擇性的參數。

如需詳細資訊,請參閱 呼叫端資訊

您可以使用 CallerMemberName 屬性避免指定成員名稱做為所呼叫方法的 String 引數。 利用這個技巧就可以避免發生 [重新命名重構] 未變更 String 值這個問題。 這特別適用于下列工作:

  • 使用追蹤和診斷常式。

  • 當繫結資料時,實作 INotifyPropertyChanged 介面。 這個介面可讓物件的屬性告知繫結的控制項屬性已變更,所以控制項可以顯示更新的資訊。 沒有 CallerMemberName 屬性 (Attribute),您就必須指定屬性 (Property) 名稱做為常值。

下圖顯示當您使用 CallerMemberName 屬性時,傳回的成員名稱。

呼叫會發生在 內 成員名稱結果
方法、屬性或事件 產生呼叫的方法、屬性或事件的名稱。
建構函式 字串 ".ctor"
靜態建構函式 字串 ".cctor"
解構函式 字串 "Finalize"
使用者定義的運算子或轉換 產生的成員名稱,例如 "op_Addition"。
屬性建構函式 套用屬性的成員名稱。 如果屬性為成員內的任何項目 (例如參數、傳回值或泛型類型參數),這個結果會是與該項目相關聯的成員名稱。
不包含成員 (例如,套用至類型) 元件層級或屬性 選擇性參數的預設值。

建構函式

CallerMemberNameAttribute()

初始化 CallerMemberNameAttribute 類別的新執行個體。

屬性

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於

另請參閱