如何避免 Caliburn Micro Event Aggregator HandleAsync 方法名称的警告?

How to avoid warnings for Caliburn Micro Event Aggregator HandleAsync method names?

我的问题很简单,如果我使用 Caliburn Micro 的事件聚合器并实现接口“IHandle”,则会创建一个名为“HandleAsync(MyEvent message, CanceleationToken token)”的方法。

我的问题是这些方法通常不是异步的,不需要标记为“async”,或者 return 任务,因此真的不应该在末尾加上“Async”,因为它们'不是异步方法,并在 Visual Studio.

中发出警告

这并没有给我带来任何技术问题,只是为了遵守 Caliburn Micro 的事件聚合器订阅方法接口而错误地命名方法让人恼火。

简单的问题是如何从方法名称的末尾删除异步,或者以其他方式摆脱这些警告?

谢谢

加里

不幸的是,目前在 Caliburn Micro 中实现 EventAggregator 是不可能的。

如果您要检查 github repository 中 EventAggregator 的实现,您会注意到它正在搜索具有特定名称的方法。

var interfaces = handler.GetType().GetTypeInfo().ImplementedInterfaces
                    .Where(x => x.GetTypeInfo().IsGenericType && x.GetGenericTypeDefinition() == typeof(IHandle<>));

foreach (var @interface in interfaces)
{
  var type = @interface.GetTypeInfo().GenericTypeArguments[0];
  var method = @interface.GetRuntimeMethod("HandleAsync", new[] { type, typeof(CancellationToken) });

  if (method != null)
  {
    _supportedHandlers[type] = method;
  }
}

一种替代方法是实现您自己的 IEventAggregator 版本并引入两个接口 IHandle<T>IHandleWithTask<T> 来表示同步和异步变体。

您应该能够suppress源代码中的警告。

在错误列表中右键单击它,然后根据要插入 #pragma 指令的位置选择抑制->在源中或抑制->在抑制文件中。