通用处理程序的集合 - 这可能吗?

时间:2017-01-17 16:58:36

标签: c# generics

使用泛型是否可以将通用集合定义为基类型并分配子类型的实例?我在下面有一个简单的代码示例,突出了我的想法和导致编译器错误的行。我知道我可以创建一个IEventHandler标记接口,并使我的通用事件处理程序继承。这将允许我将泛型类型存储在IList的集合中,但这似乎不太理想。有没有类似于我下面的代码的方法?

using System;
using System.Collections.Generic;

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            IEventHandler<SomeEvent1> handler1 = new SomeEvent1Handler();
            IEventHandler<SomeEvent2> handler2 = new SomeEvent2Handler();


            IList<IEventHandler<IEvent>> handlers = new List<IEventHandler<IEvent>>();
            // COMPILE ERROR - is this possible?
            handlers.Add(new SomeEvent1Handler());
        }

        public interface IEvent {

        }

        public interface IEventHandler<in TEvent> where TEvent : IEvent 
        {
            void Handle(TEvent someEvent);
        }

        public class SomeEvent1 : IEvent {
        }
        public class SomeEvent2 : IEvent {
        }

        public class SomeEvent1Handler : IEventHandler<SomeEvent1>
        {
            public void Handle(SomeEvent1 someEvent)
            {
                throw new NotImplementedException();
            }
        }

        public class SomeEvent2Handler : IEventHandler<SomeEvent2>
        {
            public void Handle(SomeEvent2 someEvent)
            {
                throw new NotImplementedException();
            }
        }
    }

}

2 个答案:

答案 0 :(得分:0)

  

使用泛型可以将通用集合定义为   基类型并分配子类型的实例?

是的,但只有在您的界面为IEventHandler<out TEvent>时才能完成,您无法使用in执行此操作。

如果您的代码确实有效,那么如果代码

,您会发生什么?
    public static void Main(string[] args)
    {
        IList<IEventHandler<IEvent>> handlers = new List<IEventHandler<IEvent>>();
        handlers.Add(new SomeEvent1Handler()); //Magicly works
        IEventHandler<IEvent> handler = handlers[0];
        handler.Handle(new SomeEvent2());
    }

handler表明它允许将IEvent传递给其Handle函数。这会导致SomeEvent1HandlerSomeEvent2对象传递给其public void Handle(SomeEvent1 someEvent)方法。

我的工作就是让处理程序只需要一个IEvent,在他们检查的函数中,如果它是一种他们不关心的事件,他们只能从函数返回而不做任何事情。

using System;
using System.Collections.Generic;

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            IEventHandler<SomeEvent1> handler1 = new SomeEvent1Handler();
            IEventHandler<SomeEvent2> handler2 = new SomeEvent2Handler();


            IList<IEventHandler> handlers = new List<IEventHandler>();

            handlers.Add(new SomeEvent1Handler());
        }

        public interface IEvent {

        }

        public interface IEventHandler 
        {
            void Handle(IEvent someEvent);
        }

        public class SomeEvent1 : IEvent {
        }
        public class SomeEvent2 : IEvent {
        }

        public class SomeEvent1Handler : IEventHandler
        {
            public void Handle(IEvent someEvent)
            {
                var event = someEvent as SomeEvent1;
                if(event == null)
                    return;

                //Do stuff here.
            }
        }

        public class SomeEvent2Handler : IEventHandler
        {
            public void Handle(IEvent someEvent)
            {
                var event = someEvent as SomeEvent2;
                if(event == null)
                    return;

                //Do stuff here.
            }
        }
    }

}

答案 1 :(得分:0)

这是不可能的,因为它不安全 - 如果允许你可以这样做:

import cntk as C
my_bce = label*C.log(model)+(1-label)*C.log(1-model)

我建议您在类型化处理程序周围创建一个包装类,例如

var handlers = new List<IEventHandler<IEvent>> { new SomeEvent1Handler() };
handlers[0].Handle(new SomeEvent2());

您现在可以创建public class HandlerWrapper<T> : IEventHandler<IEvent> { private readonly IEventHandler<T> inner; public HandlerWrapper(IEventHandler<T> inner) { this.inner = inner; } public void Handle(IEvent event) { if(event is T) { inner.handle((T)event); } else throw new ArgumentException("Unexpected event type"); } } ,并且需要动态管理对正确处理程序的调度。