【发布时间】:2019-01-06 01:12:53
【问题描述】:
我想使用反射来获取属性类型。 这是我的代码
var properties = type.GetProperties();
foreach (var propertyInfo in properties)
{
model.ModelProperties.Add(
new KeyValuePair<Type, string>
(propertyInfo.PropertyType.Name,
propertyInfo.Name)
);
}
这个代码propertyInfo.PropertyType.Name 没问题,但是如果我的属性类型是Nullable,我会得到这个Nullable'1 字符串,如果写FullName 如果得到这个搅拌System.Nullable1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
【问题讨论】:
-
它是 Nullable
吗? -
你想得到哪个字符串?看起来您将不得不使用 PropertyType 上的属性/方法,以允许您访问该类型的通用参数。
-
见this answer。它为泛型和非泛型类型创建一个可读的名称(例如
System.Nullable<System.Int32>)。
标签: c# reflection nullable