共用方式為


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屬性,您必須將屬性名稱指定為字面值。

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

呼叫發生在 成員名稱結果
方法、屬性或事件 呼叫的來源方法、屬性或事件名稱。
建構函式 字串 “.ctor”
靜態建構函式 字串 “.cctor”
破壞者 字串「完成」
使用者定義的運算子或轉換 成員生成的名稱,例如「op_Addition」。
屬性建構函式 應用於該屬性的成員名稱。 如果屬性是成員內的任何專案(例如參數、傳回值或泛型類型參數),則此結果就是與該專案相關聯的成員名稱。
沒有包含成員(例如組合語言層級或套用到型別的屬性) 選擇性參數的預設值。

建構函式

名稱 Description
CallerMemberNameAttribute()

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

屬性

名稱 Description
TypeId

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

(繼承來源 Attribute)

方法

名稱 Description
Equals(Object)

傳回值,這個值表示這個實例是否等於指定的物件。

(繼承來源 Attribute)
GetHashCode()

傳回這個實例的哈希碼。

(繼承來源 Attribute)
GetType()

取得目前實例的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,指出這個實例的值是否為衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,傳回值,指出這個實例是否等於指定的物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

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

(繼承來源 Object)

明確介面實作

名稱 Description
_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)

適用於

另請參閱