Наэтотвопросестьответына 堆栈溢出нарусском:Перегрузкаиндексаторастатическогокласса 为什么在C#中不允许使用静态索引器?我认为没有理由不应该允许使用它们,而且它们可能非常有用。 例如: public static class ConfigurationManager { public object this[string name] { get => ConfigurationManager.getProperty(name); set => ConfigurationManager.editProperty(name, value); } /// <summary> /// This will write the value to the property. Will overwrite if the property is already there /// </summary> /// <param name="name">Name of the property</param> /// …
我已经创建了一个供第三方使用的自定义WPF用户控件。我的控件有一个私有成员,该成员是可抛弃的,并且我想确保一旦包含窗口/应用程序关闭,它的dispose方法将始终被调用。但是,UserControl不是一次性的。我尝试实现IDisposable接口并订阅Unloaded事件,但是在主机应用程序关闭时都未调用。如果有可能,我不想依靠控件的使用者记住要调用特定的Dispose方法。 public partial class MyWpfControl : UserControl { SomeDisposableObject x; // where does this code go? void Somewhere() { if (x != null) { x.Dispose(); x = null; } } } 到目前为止,我发现的唯一解决方案是订阅Dispatcher的ShutdownStarted事件。这是合理的方法吗? this.Dispatcher.ShutdownStarted += Dispatcher_ShutdownStarted;