我有一个代码块,将类型序列化为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.我希望它忽略列表和其他自定义类型之类的所有内容.
任何人都可以建议我这样做吗?或者我是否需要指定我想要允许的类型并打开属性的类型以查看是否允许它?这有点乱,所以如果我有一个更整洁的方式会很好.
我想清楚地确定我所拥有的类型是自定义类类型(MyClass)还是Framework(System.String)提供的类型.
反思是否有任何方法可以将我的类类型与system.string或其他Framework提供的类型区分开来?
我有一个Person类:
public class Person
{
virtual public long Code { get; set; }
virtual public string Title { get; set; }
virtual public Employee Employee { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我需要一个通用的解决方案来获取Person类的所有属性,而不是自定义类类型的属性.意味着选择Code,Title属性.
typeof(Person).GetProperties(); //Title , Code , Employee
typeof(Person).GetProperties().Where(x => !x.PropertyType.IsClass); // Code
Run Code Online (Sandbox Code Playgroud)
如何在没有自定义类类型的情况下选择所有属性?(Code,Title)