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}");
                }
            }
        }
Example #2
0
        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);
        }
Example #3
0
        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);
        }
Example #4
0
        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);
        }
Example #6
0
    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;
        }
    }
        public void ListAmbiguousEvents(Type serviceType)
        {
            var typedEventHandler = typeof(TypedEventHandler <,>);

            // get all events
            var events = serviceType.GetEvents(BindingFlags.Static | BindingFlags.Public);

            string TypeName(Type type)
            {
                if (!type.IsGenericType)
                {
                    return(type.Name);
                }
                var sb = new StringBuilder();

                TypeNameSb(type, sb);
                return(sb.ToString());
            }

            void TypeNameSb(Type type, StringBuilder sb)
            {
                var name = type.Name;
                var pos  = name.IndexOf('`');

                name = pos > 0 ? name.Substring(0, pos) : name;
                sb.Append(name);
                if (!type.IsGenericType)
                {
                    return;
                }
                sb.Append("<");
                var first = true;

                foreach (var arg in type.GetGenericArguments())
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        sb.Append(", ");
                    }
                    TypeNameSb(arg, sb);
                }
                sb.Append(">");
            }

            foreach (var e in events)
            {
                // only continue if this is a TypedEventHandler
                if (!e.EventHandlerType.IsGenericType)
                {
                    continue;
                }
                var typeDef = e.EventHandlerType.GetGenericTypeDefinition();
                if (typedEventHandler != typeDef)
                {
                    continue;
                }

                // get the event args type
                var eventArgsType = e.EventHandlerType.GenericTypeArguments[1];

                // try to find the event back, based upon sender type + args type
                // exclude -ing (eg Saving) events, we don't deal with them in EventDefinitionBase (they always trigger)
                var found = EventNameExtractor.FindEvents(serviceType, eventArgsType, EventNameExtractor.MatchIngNames);

                if (found.Length == 1)
                {
                    continue;
                }

                if (found.Length == 0)
                {
                    Console.WriteLine($"{typeof(ContentService).Name}  {e.Name}  {TypeName(eventArgsType)}  NotFound");
                    continue;
                }

                Console.WriteLine($"{typeof(ContentService).Name}  {e.Name}  {TypeName(eventArgsType)}  Ambiguous");
                Console.WriteLine("\t" + string.Join(", ", found));
            }
        }
        public void GetEventName_Correct()
        {
            var eventName = EventNameExtractor.GetEventName(typeof(TestEvent));

            Assert.Equal("Test", eventName);
        }