繁体   English   中英

嵌套 static class C#

[英]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 方法适用于简单的无副作用函数(有关好的示例,请参见MathEnumerable )。 您的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM