如何在一个变量中实现不同的类类型?

时间:2017-09-13 15:26:02

标签: c#

我有3个对象类:

public class MessageData
{
    public string MessageID { get; set; }
    public string Date { get; set; }
    public bool IsSent { get; set; }
    public string EmployeeInfo { get; set; }
}

public class MultiMessageData
{
    public string MessageID { get; set; }
    public string Date { get; set; }
    public bool IsSent { get; set; }
    public byte MessageType { get; set; }
    public string MessageType { get; set; }
    public string Platform { get; set; }
    public string EmployeeInfo { get; set; }
}

public class SocialData
{
    public string MessageID { get; set; }
    public string Date { get; set; }
    public bool IsSent { get; set; }
    public bool IsReceived { get; set; }
    public byte MessageType { get; set; }
    public string MessageType { get; set; }
    public string Message { get; set; }
    public string UserName { get; set; }
}

然后我现在有一个只实现MessageData类的函数:

public void SendData()
{
    List<MessageData> data = new List<MessageData>();

    // some work here with data variable...

    _sendRepository.Send(data);
}

现在我需要的是保留大部分逻辑,但我可以实例化一个可以是任何类类型的变量,例如:

public void SendData()
{
    var data;

    if(logic == 1){
        data = new List<MessageData>();
    } else if(logic == 2){
        data = new List<MultiMessageData>();
    }else {
        data = new List<SocialData>();
    }

    // some work here...

    // if I do that then this Send method I will change it to be a Generic T maybe.
    _sendRepository.Send(data);
}

有任何线索吗?

3 个答案:

答案 0 :(得分:1)

继承是要走的路:

public class MessageDataBase
{
    public string MessageID { get; set; }
    public string Date { get; set; }
    public bool IsSent { get; set; }
}
public class MessageData : MessageDataBase
{
    public string EmployeeInfo { get; set; }
}

public class MultiMessageData : MessageDataBase
{
    public byte MessageType { get; set; }
    public string MessageTypeString { get; set; }
    public string Platform { get; set; }
    public string EmployeeInfo { get; set; }
}

public class SocialData : MessageDataBase
{
    public bool IsReceived { get; set; }
    public byte MessageType { get; set; }
    public string MessageTypeString { get; set; }
    public string Message { get; set; }
    public string UserName { get; set; }
}

答案 1 :(得分:1)

使用界面,但您必须在//..do some work here section

中进行常规更改
public interface IMessageData
{
    //Example required function for all MessageData classes
    void SetDate(DateTime date);
    void SetIsSent(bool date);
}
public abstract class BaseMessageData : IMessageData
{
    public string MessageID { get; set; }
    public string Date { get; set; }
    public bool IsSent { get; set; }

    public BaseMessageData(string messageID)
    {
        MessageID=messageID;
    }
    public void SetDate(DateTime date)
    {
        Date = date.ToString("MM/dd/yyyy");
    }
    public void SetIsSent(bool sentStatus)
    {
        IsSent = sentStatus;
    }
}
public class MessageData : BaseMessageData, IMessageData
{

    public string EmployeeInfo { get; set; }

}

public class MultiMessageData : MessageData,IMessageData
{
    public byte MessageType { get; set; }
    public string MessageType { get; set; }

    public string Platform { get; set; }
}

public class SocialData : BaseMessageData, IMessageData
{
    public bool IsReceived { get; set; }

    public byte MessageType { get; set; }
    //Can't have to properties named the same. 
    //public string MessageType { get; set; }

    public string Message { get; set; }

    public string UserName { get; set; }
}

然后在SendData

public void SendData()
{
    List<IMessageData> data;

    if (logic == 1)
    {
        data = new List<MessageData>();
    }
    else if (logic == 2)
    {
        data = new List<MultiMessageData>();
    }
    else
    {
        data = new List<SocialData>();
    }

    // some work here...
    foreach (var d in data)
    {
        //Only methods that will work are ones declared in the IMessageData interface
        d.SesDate(DateTime.Now);
        d.SetIsSent(true);
    }

    // if I do that then this Send method I will change it to be a Generic T maybe.
    _sendRepository.Send(data);
}

答案 2 :(得分:0)

您可以使用我的开源库OneOf https://github.com/mcintyre321/OneOf

OneOf是一种特殊类型,它可以容纳一系列类型中的一个值,例如F#中的Discriminated Union。它提供了使用继承/多态的替代方法。

你会写这样的方法:

public void SendData()
{
    OneOf<List<MessageData>, List<MultiMessageData>, List<SocialData>> data = null;

    if(logic == 1){
        data = new List<MessageData>();
    } else if(logic == 2){
        data = new List<MultiMessageData>();
    }else {
        data = new List<SocialData>();
    }

    // some work here...


    _sendRepository.Send(data);
}

如果您愿意,也可以var data = List<OneOf<MessageData, MultiMessageData, SocialData>>();,特别是如果您想要有不同的话 列表中的类型。

  

install-package OneOf

它支持net451和netstandard1.6