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;
}
查看全文
相关阅读:
老话题之C#写邮件发送
MVC readioButtonList的创作过程及运用
asp.net发送短信
关于MVC打印问题,打印指定的内容
Java WebSocket HttpSession与WebSocket Session的关联
JUnit accuracy/failure/stress test区别
JUnit pass/failure/error区别
Eclipse 重构
Java 注释
Java API概述
原文地址:https://www.cnblogs.com/tansm/p/83582.html
最新文章
常用JS表单验证方法
Hibernate调用存储过程和函数
JAVA中JDBC连接数据库
点击左右移动下拉选项小案例
JS分页方法
压力测试---Jemeter的使用
POI操作Excel
SpringMVC项目遇到406问题
layui+springmvc实现文件异步上传
shiro盐值加密并验证
热门文章
spring4.2.4整合ehcache
learn more ,study less(一):整体性学习策略
Shiro Web集成及拦截器机制(四)
Realm及相关对象(四)
ShiroINI配置及加密(三)
关于HttpRuntime.Cache的运用
SQL Server游标的使用
WebAPI请求——js调用
MVC中使用分部视图参数,改变分部视图连接样式
写写Web API基础
Copyright © 2011-2022 走看看