【问题标题】:Can't Find Rx Dump() Method找不到 Rx Dump() 方法
【发布时间】:2018-01-25 13:03:43
【问题描述】:

我在这里以及其他网站上的各种帖子和教程中看到了很多答案,其中Dump() 方法用于IObservable<T> 序列。但是,当我尝试使用它时,我从编译器收到... does not contain a definition for 'Dump' and no extension method for 'Dump' ... Cannot resolve symbol 'Dump' 警告。它是否已从 Rx 中删除,还是我缺少库?

【问题讨论】:

  • Dump()LinqPad中存在的扩展方法
  • 这听起来很像您看到的代码片段是从 LINQPad 复制的。 LINQPad 带来了一个.Dump()-extension 方法将对象的内容输出到它的输出窗口中。

标签: c# system.reactive


【解决方案1】:

是的,我同意它存在于 LinqPad 中的 cmets,但是您可以编写自己的,如 http://www.introtorx.com/content/v1.0.10621.0/07_Aggregation.html

public static class SampleExtentions
{
    public static void Dump<T>(this IObservable<T> source, string name)
    {
        source.Subscribe(
           i=>Console.WriteLine("{0}-->{1}", name, i), 
           ex=>Console.WriteLine("{0} failed-->{1}", name, ex.Message),
           ()=>Console.WriteLine("{0} completed", name));
    }
}

【讨论】:

    【解决方案2】:

    推荐使用LINQPad相关的nuget包(LINQPad官方提供),然后你可以使用它的Dump()方法。

    对于 .NET 核心:

    • 安装 LINQPad.Runtime

    对于 .NET 框架 4 等

    • 安装 LINQPad

    示例代码:

    using System;
    using LINQPad;
    namespace csharp_Dump_test
    {
        public class Program
        {
            public static void Main()
            {
                try
                {
                    dosome();
                }
                catch (Exception ex)
                {
                    ex.Dump();
                }
            }
    
            private static void dosome()
            {
                throw new Exception("Unable.");
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-12-13
      • 1970-01-01
      • 2011-10-01
      • 1970-01-01
      • 2016-10-22
      • 1970-01-01
      • 1970-01-01
      • 2020-05-07
      • 2020-03-16
      相关资源
      最近更新 更多