如何将不同的对象传递给同一个函数

时间:2018-05-09 23:13:29

标签: c# .net generics

我对动态数据有点失落,我希望得到你的帮助。这就是我想要做的事情:

我有以下课程:

public class SmsMessage : MessageCommonDefinition
{
    public string MessageServiceKey { get; set; }
    public string MessageAttachments { get; set; }
    public bool MessageBodyUseTemplate { get; set; }
    public string MessageBodyTemplate { get; set; }
    public string MessageBodyTemplateValues { get; set; }
    public bool ValidateWithCarrier { get; set; }
    public bool IncludeCarrierInfo { get; set; }
}

public class EmailMessage : MessageCommonDefinition
{
    public string MessageDisplayName { get; set; }
    public string MessageCc { get; set; }
    public string MessageBcc { get; set; }
    public string MessageSubject { get; set; }
    public bool MessageIsBodyHtml { get; set; }
    public bool MessageBodyUseTemplate { get; set; }
    public string MessageBodyTemplate { get; set; }
    public string MessageBodyTemplateValues { get; set; }
    public bool IsConfidential { get; set; }
    public string MessageAttachments { get; set; }
    public MailPriority MessagePriority { get; set; }
}

public abstract class MessageCommonDefinition
{
    public Guid MessageId { get; set; }
    public string CompanyCode { get; set; }
    public string MessageCode { get; set; }
    public string MessageChannelCode { get; set; }
    public string MessageTo { get; set; }
    public string MessageFrom { get; set; }
    public string MessageBody { get; set; }
    public string ChannelName { get; set; }
}

我还有一个方法来填充模板,该模板接收填充模板的消息类型(EmailMessage,SmSMessage等)以及其他参数。

这是方法:

if (triggerGroup.ToUpper().ToUpper() == AppSettingsKeys.External.ToString().ToUpper())
{
    List<PassengerTemplateEntity> passengerTemplateEntity = ACARSRepository.GetPassengerTemplateValues(emailMessage.MessageId);

    var passengerTemplateValues = DynamicClassBuilder.BuildObject();

    foreach (PassengerTemplateEntity passengerTemplate in passengerTemplateEntity)
    {
         passengerTemplateValues.AddProperty(passengerTemplate.FieldName, passengerTemplate.FieldData);
    }

    message.Body = Smart.Format(File.ReadAllText(attFilePath), passengerTemplateValues);

    message.Subject = emailMessage.IsConfidential ? SettingsManager.GetAppSettingValue(AppSettingsKeys.ConfidentialKey, "-secmsg-")
                        + " " + Smart.Format(emailMessage.MessageSubject.Trim(), passengerTemplateValues) : Smart.Format(emailMessage.MessageSubject.Trim(), passengerTemplateValues);
}
else if (triggerGroup.ToUpper() == AppSettingsKeys.Internal.ToString().ToUpper())
{
    alertTemplateValues = CoreRepository.GetExceptionMessage(emailMessage.MessageCode);

    if (alertTemplateValues != null)
    {
        message.Body = Smart.Format(File.ReadAllText(attFilePath), alertTemplateValues);
        message.Subject = Smart.Format(emailMessage.MessageSubject.Trim(), alertTemplateValues);
    }
    else
    {
        alertTemplateValues = CoreRepository.GetExceptionMessage("E127");
        throw new ArgumentException(Smart.Format("BlackBox.{0}: [Exception Code: {1}] {2}", alertTemplateValues.ShortName, alertTemplateValues.ExceptionCode, alertTemplateValues.ExceptionMessage), "ExceptionMessageCode: " + emailMessage.MessageCode, new ArgumentException());
    }
}
else
{
    alertTemplateValues = CoreRepository.GetExceptionMessage("E123");
    throw new ArgumentException(Smart.Format("BlackBox.{0}: [Exception Code: {1}] {2}", alertTemplateValues.ShortName, alertTemplateValues.ExceptionCode, alertTemplateValues.ExceptionMessage), "TriggerGroup: " + triggerGroup, new ArgumentException());
}

如您所见,该方法使用emailMessage来检索值。我想要实现的是将其用作共享功能。我想发送一个泛型参数,无论它是一个emailMessage还是一个SmsMessage,并通过发送一个通用对象并使用正确的值填充正确的类来使用该函数。

0 个答案:

没有答案