通过列表进行通用迭代
Generic iteration through list
我一直在构建电子邮件生成器功能。它最初是一个正则表达式函数,但很快就转向了反射,以使其尽可能通用。这个想法是让电子邮件生成器函数从消息数据 class 中提取信息。它开始时是一个简单的任务,因为函数只需要更改一些静态项,但是随着函数变得越来越复杂并且发送的模板需要信息表,那么我构建的函数就不够用了。
我已经扩展了使用 foreach 循环的功能 运行 通过模板并根据 messageData class 中的列表替换文本。我一直在尝试获取在 messageData 中创建的列表,以将其实现到 emailGenerator 函数中。
我有:
string value = match.Groups["value"].Value;
// Code removed to shorten length
var items = (IEnumerable) value;
但它没有从消息数据中收集信息 class。我在想也许我需要将值放入列表中?
这是 EmailGenerator 函数:
public class EmailGenerator : IEmailGenerator
{
private string mergeTemplate(string template, object obj)
{
var operationMatches = operationParser.Matches(template).Cast<Match>().Reverse().ToList();
foreach (var match in operationMatches)
{
string operation = match.Groups["operation"].Value;
string value = match.Groups["value"].Value;
var propertyInfo = obj.GetType().GetProperty(value);
object dataValue = propertyInfo.GetValue(obj, null);
if (operation == "endforeach")
{
string foreachToken = "$foreach " + value + "$";
var startIndex = template.LastIndexOf(foreachToken, match.Index);
var templateBlock = template.Substring(startIndex + foreachToken.Length, match.Index - startIndex - foreachToken.Length);
var items = (IEnumerable) value;
string blockResult = "";
foreach (object item in items)
{
blockResult += this.mergeTemplate(templateBlock, item);
}
template = template.Remove(startIndex, match.Index - startIndex).Insert(startIndex, blockResult);
}
}
}
这里是消息数据 class。它从 DTO 获取信息。
** 编辑:删除了不必要的代码。
public class messageData : IMailObject
{
public List<messageItemData> Items
{
get
{
var items = new List<messageItemData>();
foreach (var docDTO in this.recipientDTO.InfoList)
{
items.Add(new messageItemData(docDTO));
}
}
}
}
public class messageItemData
{
// Properties
}
我想要完成的是使 emailGenerator 函数变得足够通用,以便以后可用于其他电子邮件模板,从 messageData class 及其包含的列表中收集替换信息.
所以,我终于找到了答案。代码 99% 有效。就像将 var items = (IEnumerable) value;
更改为 var items = (IEnumerable) dataValue;
一样简单
我一直在构建电子邮件生成器功能。它最初是一个正则表达式函数,但很快就转向了反射,以使其尽可能通用。这个想法是让电子邮件生成器函数从消息数据 class 中提取信息。它开始时是一个简单的任务,因为函数只需要更改一些静态项,但是随着函数变得越来越复杂并且发送的模板需要信息表,那么我构建的函数就不够用了。
我已经扩展了使用 foreach 循环的功能 运行 通过模板并根据 messageData class 中的列表替换文本。我一直在尝试获取在 messageData 中创建的列表,以将其实现到 emailGenerator 函数中。 我有:
string value = match.Groups["value"].Value;
// Code removed to shorten length
var items = (IEnumerable) value;
但它没有从消息数据中收集信息 class。我在想也许我需要将值放入列表中?
这是 EmailGenerator 函数:
public class EmailGenerator : IEmailGenerator
{
private string mergeTemplate(string template, object obj)
{
var operationMatches = operationParser.Matches(template).Cast<Match>().Reverse().ToList();
foreach (var match in operationMatches)
{
string operation = match.Groups["operation"].Value;
string value = match.Groups["value"].Value;
var propertyInfo = obj.GetType().GetProperty(value);
object dataValue = propertyInfo.GetValue(obj, null);
if (operation == "endforeach")
{
string foreachToken = "$foreach " + value + "$";
var startIndex = template.LastIndexOf(foreachToken, match.Index);
var templateBlock = template.Substring(startIndex + foreachToken.Length, match.Index - startIndex - foreachToken.Length);
var items = (IEnumerable) value;
string blockResult = "";
foreach (object item in items)
{
blockResult += this.mergeTemplate(templateBlock, item);
}
template = template.Remove(startIndex, match.Index - startIndex).Insert(startIndex, blockResult);
}
}
}
这里是消息数据 class。它从 DTO 获取信息。 ** 编辑:删除了不必要的代码。
public class messageData : IMailObject
{
public List<messageItemData> Items
{
get
{
var items = new List<messageItemData>();
foreach (var docDTO in this.recipientDTO.InfoList)
{
items.Add(new messageItemData(docDTO));
}
}
}
}
public class messageItemData
{
// Properties
}
我想要完成的是使 emailGenerator 函数变得足够通用,以便以后可用于其他电子邮件模板,从 messageData class 及其包含的列表中收集替换信息.
所以,我终于找到了答案。代码 99% 有效。就像将 var items = (IEnumerable) value;
更改为 var items = (IEnumerable) dataValue;