c# – 从Nullable类型的反射中获取PropertyType.Name
发布时间:2020-12-15 03:52:09 所属栏目:百科 来源:网络整理
导读:我想使用反射获取属性类型. 这是我的代码 var properties = type.GetProperties();foreach (var propertyInfo in properties){ model.ModelProperties.Add( new KeyValuePairType,string (propertyInfo.PropertyType.Name,propertyInfo.Name) );} 这个代码pr
我想使用反射获取属性类型.
这是我的代码 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,版本= 4.0.0.0,Culture =中立,PublicKeyToken = b77a5c561934e089]] 解决方法
更改代码以查找可空类型,在这种情况下,将PropertyType作为第一个通用的agruement:
var propertyType = propertyInfo.PropertyType; if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) { propertyType = propertyType.GetGenericArguments()[0]; } model.ModelProperties.Add(new KeyValuePair<Type,string> (propertyType.Name,propertyInfo.Name)); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 【AtCoder】 ARC 103
- 将dwr集成到spring mvc(dwr的配置是基于xml)
- mainfest xml文件中的多个android.intent.action.MAIN
- 在版本控制中存储SQLite数据库是否有意义?
- 在ORACLE SQL的where子句中使用空字符串修剪
- bat命令oracle自动备份
- ruby-on-rails – Ruby on Rails在资源中寻找CSS,而不是公共
- Swift - SQLite中Data类型数据的插入、读取(BLOB类型字段)
- Cocos2d-x3.2 ClippingNode裁减节点(模板遮罩)
- 解决SQLite database is locked