2011-06-09 120 views
0

我想知道如果我能得到物體稱爲靜態類的靜態方法 例如:嵌套靜態類C#

public class Person 
{ 
    private static class TelephoneLine 
    { 
     public static string sharedString = string.Empty; 
     public static void NotifyOnCall() 
     { 
      //notify if someone call this person 
     } 
     public static void Comunicate(Person comunicateWith, string message) 
     { 
      sharedString = "Sender: " /* Object which called that method */ + " To: " + comunicateWith.Name + 
          " said: " + message; 
     } 
    } 
    public Person(string name) 
    { 
     Name = name; 
    } 
    string Name; 
    public void CallTo(Person person, string message) 
    { 
     TelephoneLine.Comunicate(person, message); 
    } 
    public void ShowCall() 
    { 
     Console.Write(TelephoneLine.sharedString); 
    } 
} 

}

那麼,有沒有可能獲得「發件人」只是路過它在ThelephoneLine.Comunicate(this,comunicateWith,msg)的參數中?

+0

如何關係到(嵌套)靜態類這個問題:

class TelephoneLine { public TelephoneLine (Person sender, Person receiver) { m_sender = sender; m_receiver = receiver; } public void Send (Person from, String message) { if (from == sender) { output "From: " + m_sender + " To: " + m_receiver + " Message: " + message; } else { output "From: " + m_receiver + " To: " + m_sender + " Message: " + message; } } Person m_sender, m_receiver; }; class Person { public void MakeCall (Person receiver, string message) { TelephoneLine line = new TelephoneLine (this, receiver); line.Send (this, message); } } 

事實上,TelephoneLine對象應該被別的東西所擁有? – CodesInChaos 2011-06-09 14:32:18

回答

2

它可以使用堆棧爬取(只要你防止你的靜態方法被內聯),但它通常是一個壞主意。

Retrieving the calling method name from within a method

做這行,如果細調試目的。這樣做是因爲你懶得把它明確寫入正常的程序流程,這是非常糟糕的設計。所以在你的情況下,我強烈建議手動傳遞它。


有點偏離主題,但我敢肯定你的類不應該是靜態的。靜態方法適用於簡單的無副作用函數(有關示例,請參見MathEnumerable)。你的TelephoneLine至少應該是一個Singleton,但IMO應該簡單地使用依賴注入並注入它的一個實例。

0

簡單的答案是否定的。很多.NET自己的事件(WinForms或ASP中的點擊等)都要求您傳遞「發件人」參數。

您可以查看堆棧以獲取方法的名稱,甚至可能是調用方的類型,但您肯定無法獲取調用該函數的特定對象。

0

的一種方法是定義者和服務之間的通用接口,並使用其合同來傳遞數據:

通用的解決方案可能是矯枉過正嵌套私人服務類,但你可能要擴大什麼可以打電話後重新公開,例如自動服務呼叫等。

public interface ICallable { string CallerIdString(); } 

public class Person : ICallable 
{ 
    private static class TelephoneLine 
    { 
     public static string sharedString = string.Empty; 
     public static void NotifyOnCall() 
     { 
      //notify if someone call this person 
     } 
     public static void Comunicate<TCaller>(TCaller Caller, Person comunicateWith, string message) where TCaller : ICallable 
     { 
      sharedString = "Sender: " + Caller.CallerIdString() + " To: " + comunicateWith.Name + 
          " said: " + message; 
          sharedString.Dump(); 
     } 
    } 
    public Person(string name) 
    { 
     Name = name; 
    } 
    string Name; 
    public void CallTo(Person person, string message) 
    { 
     TelephoneLine.Comunicate<Person>(this, person, message); 
    } 
    public void ShowCall() 
    { 
     Console.Write(TelephoneLine.sharedString); 
    } 

    public string CallerIdString() { return this.Name;} 
} 
0

的TelephoneLine類真的不應該由一個人(他們是由電話公司擁有的!)所擁有,他們真的不應該是靜態的(靜態==碼味)。

class Exchange 
{ 
    public TelephoneLine MakeCall (Person from, Person to) 
    { 
    // check that 'to' can receive call first! 
    return new TelephoneLine (from, to); 
    } 
}; 

class Person 
{ 
    public void MakeCall (Person receiver, string message) 
    { 
    TelephoneLine line = the_exchange.MakeCall (this, receiver); 
    if (line != null) 
    { 
     line.Send (this, message); 
    } 
    // else, receiver not listening! 
    } 
}