将不同的属性添加到派生类#

本文关键字:派生 添加 属性 | 更新日期: 2023-09-27 18:26:04

我有一个名为event的基类和两个名为sendEvent和receiveEvent的子类。你可以看到下面的代码:

namespace App
{
    public class Event
    {
        public Type type { get; set; }
        public Details details { get; set; }
    }
    public class Details
    {
        public string timestamp { get; set; }
        public string reference { get; set; }
        public string id { get; set; }
        public string correlator { get; set; }
        public string device1 { get; set; }
        public string device2 { get; set; }
        public string device3 { get; set; }
    }
    public class Details
    {
        public string timestamp { get; set; }
        public string reference { get; set; }
        public string primaryid { get; set; }
        public string primary_correlator { get; set; }
        public string secondaryid { get; set; }
        public string secondary_correlator { get; set; }
        public string device4 { get; set; }
        public string device5 { get; set; }
    }
    class ReceiveEvent : Event
    {
       public ReceiveEvent()
        {
            this.type = Type.Recieve;
        }

    }
    class SendEvent : Event
    {
        public SendEvent()
        {
            this.type = Type.Send;
        }
    }

    public enum Type
    {
       Send,
        Receive
    }
}

我希望sendEvent使用第一个细节,而receiveEvent使用第二个细节类。我不知道怎样才能使它成为可能。你有什么想法吗?

将不同的属性添加到派生类#

一种选择是将公共字段分离为基类并创建子类:

public class Details
{
    public string timestamp { get; set; }
    public string reference { get; set; }
}
public class SendDetails : Details
{
    public string id { get; set; }
    public string correlator { get; set; }
    public string device1 { get; set; }
    public string device2 { get; set; }
    public string device3 { get; set; }
}
public class ReceiveDetails : Details
{
    public string primaryid { get; set; }
    public string primary_correlator { get; set; }
    public string secondaryid { get; set; }
    public string secondary_correlator { get; set; }
    public string device4 { get; set; }
    public string device5 { get; set; }
}

然后使您的Event类通用:

public class Event<TDetail> where TDetail : Details
{
    public Type type { get; set; }
    public TDetail details { get; set; }
}
public class ReceiveEvent : Event<ReceiveDetails>
{
   public ReceiveEvent()
    {
        this.type = Type.Recieve;
    }
}
public class SendEvent : Event<SendDetails>
{
    public SendEvent()
    {
        this.type = Type.Send;
    }
}

通过这种方式,您可以从基本事件类或子类以强类型方式访问details

您可以创建基类Details,并从中派生出类似SendDetailsReceiveDetails的子类:

namespace App
{
  public class Event
  {
      public Type type { get; set; }
      public Details details { get; set; }
  }
  public class Details
  {
      public string timestamp { get; set; }
      public string reference { get; set; }
  }
  public class SendDetails : Details
  {
      public string id { get; set; }
      public string correlator { get; set; }
      public string device1 { get; set; }
      public string device2 { get; set; }
      public string device3 { get; set; }
  }
  public class ReceiveDetails : Details
  {
      public string primaryid { get; set; }
      public string primary_correlator { get; set; }
      public string secondaryid { get; set; }
      public string secondary_correlator { get; set; }
      public string device4 { get; set; }
      public string device5 { get; set; }
  }
  class ReceiveEvent : Event
  {
    public ReceiveEvent()
    {
       this.type = Type.Recieve;
       this.Details = new ReceiveDetails();
    }
  }
  class SendEvent : Event
  {
     public SendEvent()
     {
        this.type = Type.Send;
        this.Details = new SendDetails();
     }
  }
  public enum Type
  {
     Send,
     Receive
  }
}

如果要使用单类事件,可以使用泛型:

public class Event<TDetails>
{
    public Type type { get; set; }
    public TDetails details { get; set; }
}
class SendEvent : Event<SendDetails>
{
    public SendEvent()
    {
        this.type = Type.Send;
    }
}