【发布时间】:2015-10-07 12:45:30
【问题描述】:
我有一个如下列表(示例):
但此列表只是逻辑示例。该列表应该是动态的。列表字段可能有3个以上并且列表可以有子的集合(数据格式如json)
我想转换嵌套的 ul-li html 标签。我认为,我可以通过如下反思来做到这一点。但我第一次习惯了反射。我的代码现在就像下面一样。我该怎么办?
public static string ConvertToHtml<T>(IEnumerable<T> list) where T : class
{
StringBuilder html = new StringBuilder();
foreach (var item in list)
{
Type itemType = item.GetType();
if (itemType.IsClass)
{
FieldInfo[] fieldInfo = itemType.GetFields(BindingFlags.Public | BindingFlags.Instance); // Field?
if (fieldInfo.Any())
{
foreach (var field in fieldInfo)
{
var name = field.Name;
var value = field.GetValue(item);
}
}
PropertyInfo[] propertyInfo = itemType.GetProperties(BindingFlags.Public | BindingFlags.Instance); // Property?
if (propertyInfo.Any())
{
foreach (var property in propertyInfo)
{
var name = property.Name;
var value = property.GetValue(item);
}
}
}
}
return string.Empty;
}
【问题讨论】:
-
在服务器端执行此转换的基本原理是什么?为什么不使用前端模板框架?
标签: javascript c# html reflection converter