gpt4 book ai didi

c# - 如何动态替换电子邮件模板中的占位符

转载 作者:行者123 更新时间:2023-11-30 15:54:23 30 4
gpt4 key购买 nike

有什么好的方法可以用动态数据替换占位符吗?我尝试加载一个模板,然后用元对象中的数据替换所有 {{PLACEHOLDER}} 标签,这是有效的。但如果我需要添加更多占位符,我必须在代码中完成,并进行新的部署,所以如果可能的话,我想通过数据库来完成,就像这样:

Table Placeholders
ID, Key (nvarchar(50), Value (nvarchar(59))
1 {{RECEIVER_NAME}} meta.receiver
2 {{RESOURCE_NAME}} meta.resource
3 ..
4 .. and so on

meta 是发送到 BuildTemplate 方法的参数的名称。

因此,当我循环遍历所有占位符(来自数据库)时,我想将值从数据库转换为元对象。我需要参数中的值,而不是获取“meta.receiver”。

GetAllAsync ex.1

public async Task<Dictionary<string, object>> GetAllAsync()
{
return await _context.EmailTemplatePlaceholders.ToDictionaryAsync(x => x.PlaceholderKey, x => x.PlaceholderValue as object);
}

GetAllAsync 示例 2

public async Task<IEnumerable<EmailTemplatePlaceholder>> GetAllAsync()
{
var result = await _context.EmailTemplatePlaceholders.ToListAsync();
return result;
}

样本不使用数据库(工作)

private async Task<string> BuildTemplate(string template, dynamic meta)
{
var sb = new StringBuilder(template);

sb.Replace("{{RECEIVER_NAME}}", meta.receiver?.ToString());
sb.Replace("{{RESOURCE_NAME}}", meta.resource?.ToString());

return sb.ToString();
}

我希望它如何工作

private async Task<string> BuildTemplate(string template, dynamic meta)
{
var sb = new StringBuilder(template);

var placeholders = await _placeholders.GetAllAsync();

foreach (var placeholder in placeholders)
{
// when using reflection I still get a string like "meta.receiver" instead of meta.receiver, like the object.
// in other words, the sb.Replace methods gives the same result.
//sb.Replace(placeholder.Key, placeholder.Value.GetType().GetField(placeholder.Value).GetValue(placeholder.Value));
sb.Replace(placeholder.Key, placeholder.Value);
}

return sb.ToString();
}

我认为这可能是解决此问题的更好方法。请告诉我!

最佳答案

我们在开发中已经解决了类似的问题。

我们已经创建了扩展来格式化任何对象。

请查看我们的源代码:

public static string FormatWith(this string format, object source, bool escape = false)
{
return FormatWith(format, null, source, escape);
}

public static string FormatWith(this string format, IFormatProvider provider, object source, bool escape = false)
{
if (format == null)
throw new ArgumentNullException("format");

List<object> values = new List<object>();
var rewrittenFormat = Regex.Replace(format,
@"(?<start>\{)+(?<property>[\w\.\[\]]+)(?<format>:[^}]+)?(?<end>\})+",
delegate(Match m)
{
var startGroup = m.Groups["start"];
var propertyGroup = m.Groups["property"];
var formatGroup = m.Groups["format"];
var endGroup = m.Groups["end"];

var value = propertyGroup.Value == "0"
? source
: Eval(source, propertyGroup.Value);

if (escape && value != null)
{
value = XmlEscape(JsonEscape(value.ToString()));
}

values.Add(value);

var openings = startGroup.Captures.Count;
var closings = endGroup.Captures.Count;

return openings > closings || openings%2 == 0
? m.Value
: new string('{', openings) + (values.Count - 1) + formatGroup.Value
+ new string('}', closings);
},
RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);

return string.Format(provider, rewrittenFormat, values.ToArray());
}

private static object Eval(object source, string expression)
{
try
{
return DataBinder.Eval(source, expression);
}
catch (HttpException e)
{
throw new FormatException(null, e);
}
}

用法很简单:

var body = "[{Name}] {Description} (<a href='{Link}'>See More</a>)";
var model = new { Name="name", Link="localhost", Description="" };
var result = body.FormatWith(model);

关于c# - 如何动态替换电子邮件模板中的占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50626672/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com