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;
}
查看全文
相关阅读:
7.12函数(四)
7.11函数(三)
7.10函数(二)
7.9函数(一)
7.8文件处理补充及函数简介
7.5字符编码及文件处理
7.4数据类型及内置方法(二)
Android-------- AlertDialog中EditText无法弹出输入法的解决
Android-----输入法的显示和隐藏
Android——ExpandableListView事件拦截
原文地址:https://www.cnblogs.com/tansm/p/83582.html
最新文章
垃圾收集器与内存分配策略之篇一:简要概述和垃圾收集算法
dubbo的使用
垃圾收集器与内存分配策略
github 或者gitlab 设置添加SSH
dubbo zookeeper 的搭建
Integer比较陷阱
dubbo设计@adaptive注解的原因
vector 简单使用
Queue ,( Aizu
Stack,( Aizu
热门文章
STL
快速排序
Doubly Linked List,( Aizu
Box,( UVa, 1587 )
All in All,( UVa, 10340 )
DNA Consensus String,( UVa, 1368 )
Crosswor Answers,(UVa, 232)
7.17正则表达式与re模块
7.16模块及软件开发目录规范
7.15迭代器、生成器及常用内置方法
Copyright © 2011-2022 走看看