嵌套的静态类C#
本文关键字:静态类 嵌套 | 更新日期: 2023-09-27 17:58:42
我想知道是否可以得到一个对象,它调用了静态类的静态方法例如:
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.Communicate(this,comunicateWith,msg)的参数中传递"Sender"之外,还有什么可能得到它吗?
使用堆栈爬网是可能的(前提是防止静态方法内联),但这通常是个坏主意。
从方法中检索调用方法名称
如果可以的话,这样做是为了调试目的。这样做是因为你太懒了,没有把它明确地写进你的正常程序流中,这是非常糟糕的设计。因此,在您的情况下,我强烈建议手动传递它。
有点偏离主题,但我很确定你的类不应该是静态的。静态方法适用于简单的无副作用函数(有关好的示例,请参见Math
或Enumerable
)。你的TelephoneLine
至少应该是一个Singleton,但IMO你应该简单地使用依赖注入并注入它的一个实例。
简单的答案是否定的。许多.NET自己的事件(WinForms或ASP中的Clicked等)都需要传递一个"sender"参数。
您可以查看堆栈来获得方法的名称,甚至可能获得调用方的类型,但您肯定无法获得调用该函数的特定对象。
一种方法是在人员和服务之间定义一个通用接口,并使用其合约传递数据:
对于嵌套的私有服务类来说,通用解决方案可能有些过头了,但您可能需要扩展以后可以进行电话呼叫的内容,并将其重构为公共的,例如自动服务呼叫之类的。
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类不应该真正归Person所有(它们归电话公司所有!),也不应该是静态的(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对象应该属于其他对象:
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!
}
}