C# 为静态方法调用EventHandler
我有一个类,它有两个静态方法A()和B()。C# 为静态方法调用EventHandler,c#,static,C#,Static,我有一个类,它有两个静态方法A()和B()。 这里A()方法需要订阅一些事件,如: push.Events.OnDeviceSubscriptionExpired += new PushSharp.Common.ChannelEvents.DeviceSubscriptionExpired(Events_OnDeviceSubscriptionExpired); push.Events.OnDeviceSubscriptionIdChanged += new PushSharp.Common.
这里A()方法需要订阅一些事件,如:
push.Events.OnDeviceSubscriptionExpired += new PushSharp.Common.ChannelEvents.DeviceSubscriptionExpired(Events_OnDeviceSubscriptionExpired);
push.Events.OnDeviceSubscriptionIdChanged += new PushSharp.Common.ChannelEvents.DeviceSubscriptionIdChanged(Events_OnDeviceSubscriptionIdChanged);
我将它们保存在类的构造函数中,但实际上,当控件传递给静态方法时,它不通过构造函数,这对静态方法不起作用。这里我想要两件事:
您可以尝试在静态构造函数中执行您想要执行的操作
public class MyClass
{
static MyClass()
{
// TODO : Attach your static event handlers here
}
}
请注意,这里只能使用静态属性、字段、事件等。如果您的对象是单例,那么还有其他含义,您应该阅读Jon Skeet关于C#中单例实现的文章,以全面了解情况。必须有一个您想要订阅的已知时间点,以及另一个您想要订阅的时间点。您只需要编写一个static
Subscribe()
方法和一个staticUnsubscribe()
方法,以便在适当的时间调用。。。我可能在这里遗漏了一些东西。非常不清楚,但有一个非常强烈的暗示,这些方法不应该是静态的。