我有一个带有消息的字符串,其中包含一些我想换成实际值的字段
var message = "Hi [CustomerName]. Its [TODAY], nice to see you were born on the [DOB]!";
var mappingCodes = new List<string> {"[CUSTOMER_NAME]","[DOB]",[TODAY]};
var customEmails = new Dictionary<string, string>();
var today = DateTime.Now;
var customers = new List<Customer>()
{
new Customer()
{
FirstName = "Jo",
LastName = "Bloggs",
Email = "jo@bloggs.com",
DOB = "10/12/1960"
}
};
foreach (var customer in customers)
{
var emailMessage = "";
customEmails.Add(customer.Email,emailMessage);
}
我正在尝试做的是遍历每个客户并将消息替换为实际代码中的任何映射代码。
例如 [Today] 将是今天并且CustomerName
将是Customer.FirstName + Customer.LastName
可能有 1000 名客户,所以我需要一些强大的东西。我只是不确定如何首先检查字符串是否包含任何一个mappingCodes
,然后用所需的值替换它们。
有什么建议吗?