查找通过反射性能可空类型 [英] Find type of nullable properties via reflection
本文介绍了查找通过反射性能可空类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我检查通过反射一个对象的属性,并继续处理每个属性的数据类型。这里是我的(减少)来源:
I examine the properties of an object via reflection and continue processing the data type of each property. Here is my (reduced) source:
private void ExamineObject(object o)
{
Type type = default(Type);
Type propertyType = default(Type);
PropertyInfo[] propertyInfo = null;
type = o.GetType();
propertyInfo = type.GetProperties(BindingFlags.GetProperty |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance);
// Loop over all properties
for (int propertyInfoIndex = 0; propertyInfoIndex <= propertyInfo.Length - 1; propertyInfoIndex++)
{
propertyType = propertyInfo[propertyInfoIndex].PropertyType;
}
}
我的问题是,我最近需要处理可空。属性,但我不知道如何获得一个可空属性的类型
My problem is, that I newly need to handle nullable properties, but I have no clue how to get the type of a nullable property.
推荐答案
可能的解决方案:
propertyType = propertyInfo[propertyInfoIndex].PropertyType;
if (propertyType.IsGenericType &&
propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
propertyType = propertyType.GetGenericArguments()[0];
}
这篇关于查找通过反射性能可空类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文