zoukankan
html css js c++ java
补充一个技术文章吧,否则真的对不起博客园
/**/
///
<summary>
较安全的返回指定属性的值
</summary>
///
<remarks>
///
例如,如果你想获得Company的CompanyCode的DefaultCode属性,可以使用
///
<code>
///
object code = _company.SafeGetValue("CompanyCode.DefaultCode");
///
</code>
///
但注意:
///
传入的参数(属性名)不允许书写错误,在此方法中不对此进行检查;
///
</remarks>
public
object
SafeGetValue(
string
propertyName)
{
string
[] props
=
propertyName.Split(
'
.
'
);
string
propName;
PropertyInfo propInfo;
object
obj
=
this
;
Type objType;
for
(
int
i
=
0
; i
<
props.Length; i
++
)
{
objType
=
obj.GetType();
propName
=
props[i];
if
(propName.Trim().Length
==
0
)
throw
new
ArgumentException(
"
propertyName
"
);
try
{
propInfo
=
objType.GetProperty(propName);
if
(propInfo
==
null
)
throw
new
ArgumentException(
"
propertyName
"
);
obj
=
propInfo.GetValue(obj,
null
);
}
catch
(AmbiguousMatchException)
{
PropertyDescriptorCollection propDescs
=
TypeDescriptor.GetProperties(objType);
PropertyDescriptor propDesc
=
propDescs.Find(propName,
false
);
if
(propDesc
==
null
)
throw
new
ArgumentException(
"
propertyName
"
);
else
{
obj
=
propDesc.GetValue(obj);
}
}
if
(obj
==
null
)
return
null
;
}
return
obj;
}
查看全文
相关阅读:
pip升级报错AttributeError: 'NoneType' object has no attribute 'bytes'
在Windows中安装MySQL
nginx配置成功,浏览器无法访问
mysql 安装完以后没有mysql服务
对字符串的切片操作
linux下anaconda的安装和使用
python学习笔记
python学习笔记2018-9-18
python学习笔记2018-9-17
电脑必须用U盘引导盘才能进系统解决办法
原文地址:https://www.cnblogs.com/tansm/p/83582.html
最新文章
Mac电脑使用:通过Mac终端安装淘宝镜像报错,提示无权限的解决方法
layui table 序号
如何搭建一个vue项目(完整步骤)
记录webApp 中使用下拉刷新,上拉加载的的实例
记录使用position: fixed踩过的坑
js 多条件排序
使用lauyi-table的复选框选中行的id记录与还原选中项
js 实现浏览器禁止及允许滑动事件
css3 属性 flex 兼容 ios
js 实现 Android 的 Toast 效果
热门文章
Python运算符
Pycharm一些快捷键的使用
Python数据类型之五(集合)
Python数据类型之四(字典)
Python数据类型之三(列表)
Python中的数据类型之二(元组)
python中数据类型
python第二天 (运算符-数据类型一)
python第一天(变量-if-while-数据类型)
RuntimeError: 'cryptography' package is required for sha256_password or caching_sha2_password auth methods
Copyright © 2011-2022 走看看