您的位置:首页 > 其它

【转】NET中反射实现 可空类型 与基础类型的转换 以及获取指定属性的大小问题

2013-01-18 16:49 351 查看
/// <summary>
///
/// </summary>
/// <param name="value">要转换的值</param>
/// <param name="conversionType">要转换成的类型</param>
/// <returns></returns>
private static object ChangeType(object value, Type conversionType)
{
if (conversionType.IsGenericType && conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
if (value != null)
{
NullableConverter nullableConverter = new NullableConverter(conversionType);
conversionType = nullableConverter.UnderlyingType;
}
else
{
return null;
}
}
return Convert.ChangeType(value, conversionType);
}


反射 BindingFlags.IgnoreCase 的用法
反射属性名称,属性名称不区别大小写

PropertyInfo pi = typeof(object).GetProperty("PropertyName", BindingFlags.IgnoreCase);

这样是获取不出来的,得加上 BindingFlags.Public | BindingFlags.Instance

如下这样就可以了。

PropertyInfo info = obj.GetType().GetProperty(dc.ColumnName, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.GetProperty);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐