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;
}
查看全文
相关阅读:
Maven仓库是什么
什么是Maven
Shiro 的优点
shiro有哪些组件
Python偶斐波那契数
Python求1000以内所有3或5的倍数的和。
python"TypeError: 'NoneType' object is not iterable"错误解析
python中列表常用的几个操作函数
反射类的一些基本用法
循环随机数短时间内大多都是重复的问题
原文地址:https://www.cnblogs.com/tansm/p/83582.html
最新文章
信息搜集 & 针对src的漏洞扫描(以flash xss为例)
折腾ubuntu18
临时和永久关闭firewalld与selinux
linux系统的初化始配置
32
31
30
29
28
27
热门文章
26
25
24
23
如何重新加载 Spring Boot 上的更改,而无需重新启动服务器
什么是 JavaConfig
Spring Boot 有哪些优点
什么是 Spring Boot
Redis的特点什么是
什么是Redis
Copyright © 2011-2022 走看看