簡體   English   中英

將通用對象傳遞給C#中的方法

[英]passing generic object to a method in c#

我知道這個問題聽起來可能是重復的,但我找不到與我要描述的內容相關的任何內容。

我正在嘗試建立一個HTML電子郵件模板項目。 我有一個viewmodel(為了清楚起見,僅稱它們為類,它們只是類,具有該電子郵件通過模板中需要替換的值所必需的屬性)

 public class EmailViewModels
{
    public class AccountClosedEmailViewModel
    {
        public string Fullname { get; set; }
        public string ApplicationName { get; set; }
        public string Username { get; set; }
        public string RenewRegistrationUrl { get; set; }
    }

    public class AccountReopenedEmailViewModel
    {
        public string Fullname { get; set; }
        public string ApplicationName { get; set; }
        public string Username { get; set; }
        public string RenewRegistrationUrl { get; set; }
        public string CancelVerificationUrl { get; set; }
    }

    public class ContactFormEmailViewModel
    {
        public string Fullname { get; set; }
        public string ApplicationName { get; set; }
    }
    //rest removed for brevity
}

我想要一個采用電子郵件視圖模型的方法,它可以是上面類中定義的任何方法,因此我有一個通用方法來消除我對每種視圖模型類型都擁有BuildHtmlFromTemplate。

public string BuildHtmlFromTemplate(templateFilePath, passing any one of the email viewmodels defined in the class)
{
    var temp = File.IO.ReadAllText(templateFilePath);
    temp.Replace("{username}", modelipassedin.username);
    //do rest
    return temp;

}

這可能嗎?

您可以像這樣使用反射和泛型

public string BuildHtmlFromTemplate<T>(templateFilePath, T o)
{
    var temp = File.IO.ReadAllText(templateFilePath);
    foreach(var prop in typeof(T).GetProperties()){
        temp = temp.Replace("{"+prop.Name+"}", prop.GetValue(o));
    }
    return temp;
}

或沒有泛型

public string BuildHtmlFromTemplate(templateFilePath, object o)
{
    var temp = File.IO.ReadAllText(templateFilePath);
    foreach(var prop in o.GetType().GetProperties()){
        temp = temp.Replace("{"+prop.Name+"}", prop.GetValue(o));
    }
    return temp;
}

如果想通過名稱訪問對象屬性,而不管它們的類型如何,則可以:

  1. 使用反射,或

  2. 將所有課程變成詞典。

要使用反射通過名稱獲取屬性的值,可以使用類似以下內容的方法:

public static object GetValue(this object @this, string property)
{
    return @this.GetType().GetProperty(property).GetValue(@this, null);
}

然后,您可以使用正則表達式一次匹配並替換占位符:

var inputHtml = @"<h1>{title}</h1> <p>{text}</p>";
var instance = new
{
    title = "some title",
    text = "some text"
};

// note: not much error checking here, and
// placeholder casing is important 

var regex = new Regex("{(.*?)}");
var outputHtml = regex.Replace(inputHtml, m =>
{
    var placeholder = m.Groups[1].Value;

    var prop = instance.GetType().GetProperty(placeholder);
    if (prop == null)
        return "";

    return prop.GetValue(instance).ToString();
});

對於第二種方法,您只需將數據存儲為KeyValuePair<string, string>並進行一些簡單的字符串替換。

public class EmailViewModel
{
  public string ToHTMLString()
  {
     //And use reflection on the properties here.
  }
}

public class AccountClosedEmailViewModel:EmailViewModel    {
    public string Fullname { get; set; }
    public string ApplicationName { get; set; }
    public string Username { get; set; }
    public string RenewRegistrationUrl { get; set; }
}

public class AccountReopenedEmailViewModel:EmailViewModel{
    public string Fullname { get; set; }
    public string ApplicationName { get; set; }
    public string Username { get; set; }
    public string RenewRegistrationUrl { get; set; }
    public string CancelVerificationUrl { get; set; }
}

public class ContactFormEmailViewModel:EmailViewModel    {
    public string Fullname { get; set; }
    public string ApplicationName { get; set; }
}

編輯:這樣,您可以移動基類中的一些常用屬性,以使每個模型更輕一些。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM