C# 在非泛型类之上实现泛型

C# 在非泛型类之上实现泛型,c#,generics,C#,Generics,我有两个由实体框架生成的非泛型类: public partial class Event { public Event() { this.Notifications = new HashSet<Notification>(); } public int Id { get; private set; } public string Name { get; priva

我有两个由实体框架生成的非泛型类:

    public partial class Event
    {
        public Event()
        {
            this.Notifications = new HashSet<Notification>();
        }

        public int Id { get; private set; }
        public string Name { get; private set; }
        public string Description { get; private set; }

        internal virtual ICollection<Notification> Notifications { get; set; }
    }

    public abstract partial class Notification
    {
        private System.Guid Id { get; set; }

        public abstract void Deliver(object eventContext);
    }
公共部分类事件
{
公共活动()
{
this.Notifications=newhashset();
}
public int Id{get;private set;}
公共字符串名称{get;private set;}
公共字符串说明{get;private set;}
内部虚拟ICollection通知{get;set;}
}
公共抽象部分类通知
{
private System.Guid Id{get;set;}
公共抽象void Deliver(objecteventcontext);
}
void Deliver(objecteventcontext)的实现不需要传入特定类型(它们使用反射从eventContext提取信息),但同时,我希望强制类的用户提供特定类型。为了实现这一点,我决定最好在这些类之上实现一个通用层:

public class Notification<TContext>
{
    private Notification internalNotification;

    public Notification(Notification notification)
    {
        if (notification == null)
            throw new ArgumentNullException("notification");

        this.internalNotification = notification;
    }

    public void Deliver(TContext context)
    {
        this.internalNotification.Deliver(context);
    }
}

public class Event<TContext> : Event
{
    private Event internalEvent;

    internal Event(Event evt)
    {
        if (evt == null)
            throw new ArgumentNullException("evt");

        this.internalEvent = evt;
    }

    public IEnumerable<Notification<TContext>> GetNotifications()
    {
        foreach (var notification in internalEvent.Notifications)
            yield return new Notification<TContext>(notification);
    }
}
公共类通知
{
私人通知;
公共通知(通知)
{
如果(通知==null)
抛出新的异常(“通知”);
this.internalNotification=通知;
}
public void Deliver(TContext上下文)
{
this.internalNotification.Deliver(上下文);
}
}
公共类事件:事件
{
私人事件;
内部事件(事件evt)
{
如果(evt==null)
抛出新的ArgumentNullException(“evt”);
this.internalEvent=evt;
}
公共IEnumerable GetNotifications()
{
foreach(internalEvent.Notifications中的var通知)
收益返还新通知(通知);
}
}
这样,我可以创建类似以下内容的代码:

public static class Events
{
     public static Event<PaymentContext> PaymentEvent()
     {
        ... get Event object from EF...

        return new Event<PaymentContext>(event);
     }

     public static Event<HireContext> EmployeeHiredEvent()
     {
          .... get Event object from EF...

          return new Event<HireContext>(event);
     }
}
公共静态类事件
{
公共静态事件PaymentEvent()
{
…从EF获取事件对象。。。
返回新事件(事件);
}
公共静态事件EmployeeHiredEvent()
{
..从EF获取事件对象。。。
返回新事件(事件);
}
}
然后,用户将被迫将适当的类型传递给Deliver(TContext)方法

这适用于我的情况,但我不喜欢我的泛型通知和事件类由非泛型类组成的方式——我觉得它们应该从非泛型版本继承。但当然,从entity framework接收的实体是基类,而不是派生的泛型类,因此需要进行某种转换才能将其转换为基类(理想情况下,我希望将其转换为子类型,但我认为这是不可能的)


有没有更好的方法来实现这个“非泛型类上的泛型层”解决方案。

恭喜你,你已经发现为什么让一个外部工具为你生成分部类不是一个好主意。:)我对EF了解不多,但看看是否可以让它生成具有更类型安全签名的方法,或者让它使用泛型子类而不是这些基。