嵌套静态类C#

嵌套静态类C#,c#,nested-class,static-class,C#,Nested Class,Static Class,我想知道我是否能得到一个叫做静态类的静态方法的对象 例: } 所以,除了在ephoneline.com的参数中传递“Sender”之外,还有可能获取“Sender”吗 可以使用堆栈爬网(前提是防止静态方法内联),但这通常不是一个好主意 如果可以,则出于调试目的执行此操作。这样做是因为您太懒了,无法将其显式地写入到您的正常程序流中,这是非常糟糕的设计。因此,在您的情况下,我强烈建议手动传递它 有点离题,但我很确定你的类不应该是静态的。静态方法适用于简单的无副作用函数(有关良好示例,请参见Mat

我想知道我是否能得到一个叫做静态类的静态方法的对象 例:

}


所以,除了在ephoneline.com的参数中传递“Sender”之外,还有可能获取“Sender”吗

可以使用堆栈爬网(前提是防止静态方法内联),但这通常不是一个好主意

如果可以,则出于调试目的执行此操作。这样做是因为您太懒了,无法将其显式地写入到您的正常程序流中,这是非常糟糕的设计。因此,在您的情况下,我强烈建议手动传递它



有点离题,但我很确定你的类不应该是静态的。静态方法适用于简单的无副作用函数(有关良好示例,请参见
Math
Enumerable
)。您的
电话线
至少应该是一个单例,但在我看来,您应该简单地使用依赖项注入并注入它的一个实例。

简单的答案是否定的。许多.NET自身的事件(在WinForms或ASP中单击等)要求您传递“发件人”参数


您可以查看堆栈以获得方法的名称,甚至可能是调用方的类型,但您肯定无法获得调用函数的特定对象。

一种方法是在人员和服务之间定义一个公共接口,并使用其契约传递数据:

一般的解决方案对于嵌套的私有服务类来说可能有些过分,但您可能希望在以后可以拨打电话的方面进行扩展,并将其重构为公共的,例如自动服务呼叫之类的

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;}
}
公共接口可调用{string CallerIdString();}
公共类人员:iCalable
{
专用静态类电话线
{
公共静态字符串sharedString=string.Empty;
公共静态void NotifyOnCall()
{
//如果有人打电话给此人,请通知
}
公共静态void通信(TCaller调用者、联系人、字符串消息),其中TCaller:ICallable
{
sharedString=“发件人:”+Caller.CallerIdString()+”至:“+ComCommunicationWith.Name”+
"说:"信息,;
sharedString.Dump();
}
}
公众人物(字符串名称)
{
名称=名称;
}
字符串名;
public void CallTo(个人、字符串消息)
{
电话线。通信(这个、人、信息);
}
公共void ShowCall()
{
控制台。写入(电话线。共享字符串);
}
公共字符串CallerIdString(){返回this.Name;}
}

电话线类不应该真正归个人所有(它们归电话公司所有!),也不应该是静态的(static==代码气味)

事实上,电话线对象应该属于其他对象:

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!
  }
}

这个问题与(嵌套的)静态类有什么关系?
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);
  }
}
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!
  }
}