C# .NET Rx- Where is System.Reactive?

有些话、适合烂在心里 提交于 2019-12-03 10:40:47

问题


I have an intensive Java background so forgive me if I'm overlooking something obvious in C#, but my research is getting me nowhere. I am trying to use the reactive Rx .NET library. The compiler is not complaining about the IObservable but it is with the call to the zip method. It is throwing the "... are you missing a using directive or assembly reference?"

I've been going through the namespaces and I cannot find what is looking for. I cannot find the System.Reactive which also throws an error if used, and all the references are already included for this Windows 8.1 app. Can someone please give me a lead on what is wrong?

public sealed class EventEngine
{    
    private static readonly EventEngine singleton = new EventEngine();

    public static EventEngine get()
    {
        return singleton;
    }

    public IObservable<MusicNote> CurrentKey { get; set; }
    public IObservable<Scale> CurrentScale { get; set; }

    public IObservable<AppliedScale> CurrentAppliedScale
    {
        get
        {
            return CurrentScale.zip(CurrentKey,
                (s, k) => AppliedScale.getAppliedScale(k, s));
        } 
    }

    private EventEngine() {}
}

*UPDATE *

Here is the working version after considering input from answers.

public sealed class EventEngine
{
    private static readonly EventEngine singleton = new EventEngine();

    public static EventEngine get()
    {
        return singleton;
    }

    public IObservable<MusicNote> CurrentKey { get; set; }
    public IObservable<Scale> CurrentScale { get; set; }

    public IObservable<AppliedScale> CurrentAppliedScale
    {
        get
        {
            return Observable.Zip(CurrentScale, CurrentKey,
                (s, k) => AppliedScale.getAppliedScale(s,k));
        } 
    }

    private EventEngine() {}
}

回答1:


You have probably not added the necessary assembly references for Rx to your project. (Referencing an assembly is not the same thing as importing a namespace! You already know what a namespace is; an assembly is something similar to a JAR; the smallest unit of code deployment/distribution. Your project must reference it before the namespaces defined inside it become available for use.)

The compiler likely doesn't complain about IObservable<T> and IObserver<T> because your project is targeting .NET Framework version 4 or later. These two interfaces have been part of the core .NET Framework Class Library (FCL) since .NET version 4. (If you targeted an earlier .NET version, you'd get errors for using these undefined interfaces, too.)

Every part of Rx other than these two interfaces is not included in the core .NET FCL, but resides in their own (add-on) assemblies. You can add them to your project e.g. by installing the corresponding NuGet packages:

  1. In Visual Studio, go to ToolsNuGet Package ManagerPackage Manager Console.

  2. In the NuGet console window, select the target project (where you want to use Rx) in the drop-down list Default project.

  3. Next, type Install-Package System.Reactive and hit Enter ↵. (Note: This package used to be called Rx-Main previously; see this answer for details.)

  4. This will add the System.Reactive.* assembly references to your project.




回答2:


As of version 3, the Rx project is now known as System.Reactive because it "brings the NuGet package naming in line with NuGet guidelines and also the dominant namespace in each package."

It can be downloaded from NuGet by searching for "System.Reactive" or you can download it here: https://www.nuget.org/packages/System.Reactive/

Here is the project's GitHub page: https://github.com/Reactive-Extensions/Rx.NET

Package Mappings

  • Rx-Main System.Reactive
  • Rx-Core System.Reactive.Core
  • Rx-Interfaces System.Reactive.Interfaces
  • Rx-Linq System.Reactive.Linq
  • Rx-PlatformServices System.Reactive.PlatformServices
  • Rx-Testing Microsoft.Reactive.Testing

Source



来源:https://stackoverflow.com/questions/30111878/c-sharp-net-rx-where-is-system-reactive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!