我有一个代码块,将类型序列化为Html标记.
Type t = typeof(T); // I pass <T> in as a paramter, where myObj is of type T
tagBuilder.Attributes.Add("class", t.Name);
foreach (PropertyInfo prop in t.GetProperties())
{
object propValue = prop.GetValue(myObj, null);
string stringValue = propValue != null ? propValue.ToString() : String.Empty;
tagBuilder.Attributes.Add(prop.Name, stringValue);
}
Run Code Online (Sandbox Code Playgroud)
这个伟大的工程,但我希望它只是对基本类型,像这样做int,double,bool等,以及其他类型的不是原始的,但可以很容易地连载一样string.我希望它忽略列表和其他自定义类型之类的所有内容.
任何人都可以建议我这样做吗?或者我是否需要指定我想要允许的类型并打开属性的类型以查看是否允许它?这有点乱,所以如果我有一个更整洁的方式会很好.