Objet<T> , constructeur
Initialise une nouvelle instance de la classe Subject<T>.
Espace de noms :System.Reactive.Subjects
Assemblée: System.Reactive (en System.Reactive.dll)
Syntaxe
'Declaration
Public Sub New
'Usage
Dim instance As New Subject()
public Subject()
public:
Subject()
new : unit -> Subject
public function Subject()
Notes
Crée un instance d’objet de type T
Exemples
using System;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Reactive.Concurrency;
using System.Threading;
namespace Example
{
class Program
{
static void Main()
{
//*****************************************************************************************************//
//*** ***//
//*** A subject acts similar to a proxy in that it acts as both a subscriber and a publisher ***//
//*** It's IObserver interface can be used to subscribe to multiple streams or sequences of data. ***//
//*** The data is then published through it's IObservable interface. ***//
//*** ***//
//*** In this example a simple string based subject is used to subscribe to multiple news feeds ***//
//*** that provide random news headlines. Subscribers can then subscribe to the subject's ***//
//*** observable interface to observe the data stream(s) or a subset ofthe stream(s). Below we ***//
//*** subscribe the subject to two different news headline feeds. Then two subscriptions are ***//
//*** created: one for delivery of all news headlines, the other receives only local news headlines ***//
//*** ***//
//*** A local news headline just contains the newsLocation substring ("in your area."). ***//
//*** ***//
//*****************************************************************************************************//
Subject<string> mySubject = new Subject<string>();
//*********************************************************//
//*** Create news feed #1 and subscribe mySubject to it ***//
//*********************************************************//
NewsHeadlineFeed NewsFeed1 = new NewsHeadlineFeed("Headline News Feed #1");
NewsFeed1.HeadlineFeed.Subscribe(mySubject);
//*********************************************************//
//*** Create news feed #2 and subscribe mySubject to it ***//
//*********************************************************//
NewsHeadlineFeed NewsFeed2 = new NewsHeadlineFeed("Headline News Feed #2");
NewsFeed2.HeadlineFeed.Subscribe(mySubject);
Console.WriteLine("Subscribing to news headline feeds.\n\nPress ENTER to exit.\n");
//*****************************************************************************************************//
//*** Create a subscription to the subject's observable sequence. This subscription will receive ***//
//*** all headlines. ***//
//*****************************************************************************************************//
IDisposable allNewsSubscription = mySubject.Subscribe(x =>
{
Console.WriteLine("Subscription : All news subscription\n{0}\n", x);
});
//*****************************************************************************************************//
//*** Create a subscription to the subject's observable sequence. This subscription will filter for ***//
//*** only local headlines. ***//
//*****************************************************************************************************//
IDisposable localNewsSubscription = mySubject.Where(x => x.Contains("in your area.")).Subscribe(x =>
{
Console.WriteLine("\n************************************\n" +
"***[ Local news headline report ]***\n" +
"************************************\n{0}\n\n", x);
});
Console.ReadLine();
//*********************************//
//*** Cancel both subscriptions ***//
//*********************************//
allNewsSubscription.Dispose();
localNewsSubscription.Dispose();
}
}
//*********************************************************************************//
//*** The NewsHeadlineFeed class is just a mock news feed in the form of an ***//
//*** observable sequence in Reactive Extensions. ***//
//*********************************************************************************//
class NewsHeadlineFeed
{
private string feedName; // Feedname used to label the stream
private IObservable<string> headlineFeed; // The actual data stream
private readonly Random rand = new Random(); // Used to stream random headlines.
//*** A list of predefined news events to combine with a simple location string ***//
static readonly string[] newsEvents = { "A tornado occurred ",
"Weather watch for snow storm issued ",
"A robbery occurred ",
"We have a lottery winner ",
"An earthquake occurred ",
"Severe automobile accident "};
//*** A list of predefined location strings to combine with a news event. ***//
static readonly string[] newsLocations = { "in your area.",
"in Dallas, Texas.",
"somewhere in Iraq.",
"Lincolnton, North Carolina",
"Redmond, Washington"};
public IObservable<string> HeadlineFeed
{
get { return headlineFeed; }
}
public NewsHeadlineFeed(string name)
{
feedName = name;
//*****************************************************************************************//
//*** Using the Generate operator to generate a continous stream of headline that occur ***//
//*** randomly within 5 seconds. ***//
//*****************************************************************************************//
headlineFeed = Observable.Generate(RandNewsEvent(),
evt => true,
evt => RandNewsEvent(),
evt => { Thread.Sleep(rand.Next(5000)); return evt; },
Scheduler.ThreadPool);
}
//****************************************************************//
//*** Some very simple formatting of the headline event string ***//
//****************************************************************//
private string RandNewsEvent()
{
return "Feedname : " + feedName + "\nHeadline : " + newsEvents[rand.Next(newsEvents.Length)] +
newsLocations[rand.Next(newsLocations.Length)];
}
}
}