相关疑难解决方法(0)

如何测试类型是否为原始

我有一个代码块,将类型序列化为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.我希望它忽略列表和其他自定义类型之类的所有内容.

任何人都可以建议我这样做吗?或者我是否需要指定我想要允许的类型并打开属性的类型以查看是否允许它?这有点乱,所以如果我有一个更整洁的方式会很好.

c# reflection primitive-types

154
推荐指数
6
解决办法
8万
查看次数

标签 统计

c# ×1

primitive-types ×1

reflection ×1