[英]Nested static class C#
我想知道我是否可以獲得 object ,它稱為 static 方法的 static ZA2F2ED4F8EBC0CAB61C21A29
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) 的參數中傳遞它之外,還有沒有可能獲得“Sender”?
可以使用堆棧爬行(前提是您防止 static 方法被內聯),但這通常是個壞主意。
如果可以的話,出於調試目的這樣做。 這樣做是因為您懶得將其顯式寫入您的正常程序流程中,這是非常糟糕的設計。 所以在你的情況下,我強烈建議手動傳遞它。
有點離題,但我很確定你的課程不應該是 static。 Static 方法適用於簡單的無副作用函數(有關好的示例,請參見Math
或Enumerable
)。 您的TelephoneLine
至少應該是 Singleton,但 IMO 您應該簡單地使用依賴注入並注入它的單個實例。
簡單回答是不。 許多 .NET 自己的事件(WinForms 或 ASP 中的 Clicked 等)要求您傳遞“發送者”參數。
您可以查看堆棧以獲取方法的名稱,甚至可能是調用者的類型,但您肯定無法獲取調用 function 的特定 object。
一種方法是在人員和服務之間定義一個通用接口,並使用它的合約來傳遞數據:
通用解決方案可能對嵌套的私有服務 class 來說太過分了,但您可能希望擴展以后可以撥打電話並將其公開的內容,例如自動服務調用等。
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;}
}
TelephoneLine class 不應該真正歸個人所有(它們歸電話公司所有。)並且它們真的不應該是 static(靜態 == 代碼氣味)。
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 object 應該歸其他人所有:
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!
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.