嵌套静态类C#

时间:2011-06-09 14:27:15

标签: c# nested-class static-class

我想知道我是否可以获得静态类静态方法的对象 例如:

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”?

4 个答案:

答案 0 :(得分:2)

可以使用堆栈抓取(假设您阻止静态方法被内联),但这通常是一个坏主意。

Retrieving the calling method name from within a method

如果正常,请执行此操作以进行调试。这样做是因为你懒得将它明确写入正常的程序流程中是非常糟糕的设计。因此,在您的情况下,我强烈建议您手动传递它。


有点偏离主题,但我很确定你的课程不应该是静态的。静态方法适用于简单的无副作用函数(有关示例,请参阅MathEnumerable)。你的TelephoneLine至少应该是一个Singleton,但你应该只使用依赖注入并注入一个实例。

答案 1 :(得分:0)

简单的答案是否定的。很多.NET自己的事件(WinForms或ASP中的Clicked等)都要求你传递'sender'参数。

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

答案 2 :(得分: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;}
}

答案 3 :(得分:0)

TelephoneLine类不应该真正归个人所有(它们由电话公司拥有!)它们实际上不应该是静态的(静态==代码味道)。

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