繁体   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