public sealed class FtpManager
{
public event EventHandler LoggingIn = delegate { };
private void OnLoggingIn(object sender, EventArgs e)
{
var handler = LoggingIn;
handler(sender, e);
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我LoggingIn用一个空委托初始化了事件处理程序.
这会以任何方式影响使用的内存空间吗?特别是当有数百或数千个事件宣布这样的方式?
有没有什么聪明的方法可以避免在以一般方式调用事件之前测试事件的无效性?很明显,如果我调用一个委托,我希望它被分配.
(如果我真的想要/需要测试它的无效性,我最终可以明确地做到这一点,但系统地进行这种测试有点单调乏味.)
public delegate void ResetTradesDelegate();
public ResetTradesDelegate ResetTradesEvents;
public void OnSessionRxAdmMessage(IVfxFixAppSession session, FixMessage msg)
{
if (ResetTradesEvent != null) //<-- Is there "any" a way not to write this test explicitly for each delegate ?
ResetTradesEvent();
}
Run Code Online (Sandbox Code Playgroud) 可能重复:
在事件声明中添加匿名空委托是否有缺点?
使用事件处理程序(在C#中)时,以下模式非常常见:
public event Action handler;
…
// some method:
if(handler != null) handler();
Run Code Online (Sandbox Code Playgroud)
为此事件分配空委托是否有任何缺点?这将在if !=null任何地方保存条件,事件被触发.当然,这只适用于我们无法保证事件总是被赋予适当的委托.
public event Action handler;
…
// in constructor:
handler += ()=>{};
…
// some method:
handler();
Run Code Online (Sandbox Code Playgroud)
当然,性能略有下降,但它使代码更清晰.在这种情况下,最佳做法是什么?任何技术缺点?
调用委托时,您始终必须检查它是否为空.这通常是导致错误的原因.由于委托或多或少只是一个函数列表,我认为这可以由委托本身轻松检查.
有谁知道,为什么它已经实施了?
我刚刚在程序中遇到一个错误,我正在编写一个异常被抛出的状态,说明"对象引用必须设置为对象的实例".经过调查,我发现在尝试触发事件时抛出了此异常,但事件没有添加任何委托方法.
我想检查一下我的理解是否正确,作为开发人员,如果没有先检查事件是否等于null,就不应该触发事件?例如:
if (this.MyEventThatIWantToFire != null)
{
this.MyEventThatIWantToFire();
}
Run Code Online (Sandbox Code Playgroud)
提前感谢您的建议/想法.
这听起来很疯狂,但我在课堂上创建了一个活动,试图在没有任何人注册的情况下提高它.但是它会给出一个例外.在提出异常之前是否有必要让某人注册?如果是,那么有什么工作呢?
习惯于VB.NET,我习惯于"只是举起事件".当然,自定义事件有所不同,但是"常规"事件 - 我不需要Nothing在提升之前检查代表是否是.
使用C#,我发现自己重复这种模式:
if (myHandler != null)
{
myHandler(this, new EventArgs());
}
Run Code Online (Sandbox Code Playgroud)
我以为以下模式可能会更优雅:
myHandler = (sender, e) => { };myHandler(this, new EventArgs());这种模式会比前一种模式更多或更少吗?我还应该考虑其他重要因素吗?
有人可以解释delegate{ }下面的含义
public event EventHandler CanExecuteChanged = delegate { };
Run Code Online (Sandbox Code Playgroud)
另外,为什么它不需要对CanExecuteChanged进行空引用检查,以及使用它的潜在性能命中(如果有的话).
如果事件没有订阅者,我如何确保在触发事件时不会抛出异常.
// Delegate declaration
public delegate void _delDisplayChange(object sender,string option);
// Event declaration
public event _delDisplayChange DisplayChange;
//throwing the event
DisplayChange(this, "DISTRIBUTION");
Run Code Online (Sandbox Code Playgroud) 我有点厌倦了在代码中发现所有这些无用的噪音:
private void RaiseSomeOtherEventIfItIsNotNull()
{
if (this.SomeOtherEvent != null)
{
this.SomeOtherEvent(this, EventArgs.Empty);
}
}
Run Code Online (Sandbox Code Playgroud)
在99.9%的案例中,我不在乎是否有人附加或者是否为空.刚举起活动!我真的不明白为什么c#编译器会让我写出所有这些噪音.
所以我虽然可以声明这样的事件:
public event EventHandler SomeOtherEvent = delegate { };
Run Code Online (Sandbox Code Playgroud)
这将允许我摆脱无用的空检查和无用的Raise*方法.我可以随时做:
this.SomeOtherEvent(this, EventArgs.Empty);
Run Code Online (Sandbox Code Playgroud)
现在,当我在LutzRöder的Reflector中将标准方法与"我的"方法进行比较时,我发现了一些显着的差异.编译器已覆盖,Add{}并且Remove{}匿名委托有一个额外的静态实例:
[CompilerGenerated]
private static EventHandler CS$<>9__CachedAnonymousMethodDelegate1;
Run Code Online (Sandbox Code Playgroud)
并且有这样的:
.method private hidebysig static void <.ctor>b__0(object, class [mscorlib]System.EventArgs) cil managed
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor()
.maxstack 8
L_0000: nop
L_0001: ret
}
Run Code Online (Sandbox Code Playgroud)
现在我的问题:您是否在使用默认初始化这样的十进制事件中看到了任何问题或缺点?