public void List_Ambiguous_Events() { EventInfo[] events = MediaService.GetType().GetEvents(BindingFlags.Static | BindingFlags.Public); Type typedEventHandler = typeof(TypedEventHandler <,>); foreach (EventInfo e in events) { // only continue if this is a TypedEventHandler if (!e.EventHandlerType.IsGenericType) { continue; } Type typeDef = e.EventHandlerType.GetGenericTypeDefinition(); if (typedEventHandler != typeDef) { continue; } // get the event arg type Type eventArgType = e.EventHandlerType.GenericTypeArguments[1]; Attempt <EventNameExtractorResult> found = EventNameExtractor.FindEvent(typeof(MediaService), eventArgType, EventNameExtractor.MatchIngNames); if (!found.Success && found.Result.Error == EventNameExtractorError.Ambiguous) { Console.WriteLine($"Ambiguous event, source: {typeof(MediaService)}, args: {eventArgType}"); } } }
public void No_Match() { Attempt <EventNameExtractorResult> found = EventNameExtractor.FindEvent(this, new SaveEventArgs <double>(0), EventNameExtractor.MatchIngNames); Assert.IsFalse(found.Success); Assert.AreEqual(EventNameExtractorError.NoneFound, found.Result.Error); }
public void Ambiguous_Match() { Attempt <EventNameExtractorResult> found = EventNameExtractor.FindEvent(this, new SaveEventArgs <int>(0), EventNameExtractor.MatchIngNames); Assert.IsFalse(found.Success); Assert.AreEqual(EventNameExtractorError.Ambiguous, found.Result.Error); }
public void Find_Event_Non_Ing() { Attempt <EventNameExtractorResult> found = EventNameExtractor.FindEvent(this, new SaveEventArgs <string>("test"), EventNameExtractor.MatchNonIngNames); Assert.IsTrue(found.Success); Assert.AreEqual("FindingMe", found.Result.Name); }
public void Find_Event_Ing() { var found = EventNameExtractor.FindEvent(this, new SaveEventArgs <string>("test"), EventNameExtractor.MatchIngNames); Assert.IsTrue(found.Success); Assert.AreEqual("FoundMe", found.Result.Name); }
protected EventDefinitionBase(object?sender, object?args, string?eventName = null) { Sender = sender ?? throw new ArgumentNullException(nameof(sender)); Args = args ?? throw new ArgumentNullException(nameof(args)); EventName = eventName; if (EventName.IsNullOrWhiteSpace()) { // don't match "Ing" suffixed names Attempt <EventNameExtractorResult?> findResult = EventNameExtractor.FindEvent(sender, args, EventNameExtractor.MatchIngNames); if (findResult.Success == false) { throw new AmbiguousMatchException( "Could not automatically find the event name, the event name will need to be explicitly registered for this event definition. " + $"Sender: {sender.GetType()} Args: {args.GetType()}" + " Error: " + findResult.Result?.Error); } EventName = findResult.Result?.Name; } }