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;
}
查看全文
相关阅读:
[LeetCode in Python] 98 (M) validate binary search tree 验证二叉搜索树
[LeetCode in Python] 79 (M) word search 单词搜索
mybatis3源码阅读之SqlSessionFactoryBuilder
java简单的双色球摇号程序
低延时直播应用 流媒体
glPixelStorei 详解 包括像素传输
depth/stencil buffer的作用 ----------理解模板缓存 opengl
实用网站汇编
利用CodeBlocks结合freeglut快速搭建OpenGL开发环境
opengl 4.5 中文api 链接
原文地址:https://www.cnblogs.com/tansm/p/83582.html
最新文章
动态时间规整DTW
Linux用脚本守护进程
[面试]美团点评面试记录
[面试]百度搜索架构部面试记录
[python]python进阶编程(7)-python网络编程
[python]python进阶编程(6)-多进程和多线程
[python]python进阶编程(5)-异常的处理
[python]python进阶编程(4)-动态类和动态方法的创建和调用
[python]python进阶编程(3)-装饰器
[python]python进阶编程(2)-匿名函数和闭包
热门文章
[python]python的可变对象和不可变对象
[python]python进阶编程(1)-生成迭代的用法
[LeetCode in Python] 5403 (H) find the kth smallest sum of a matrix with sorted rows 有序矩阵中的第 k 个最小数组和
[LeetCode in Python] 5402 (M) longest continuous subarray with absolute diff less than or equal to limit 绝对差不超过限制的最长连续子数组
[LeetCode in Python] 1425 (H) constrained subsequence sum 带限制的子序列和
[LeetCode in Python] 239 (H) sliding window maximum 滑动窗口最大值
[LeetCode in Python] 53 (E) maximum-subarray 最大子序和
[LeetCode in Python] 56
[LeetCode in Python] 5393 (M) maximum points you can obtain from cards 可获得的最大点数
[LeetCode in Python] 5392 (E) maximum score after splitting a string 分割字符串的最大得分
Copyright © 2011-2022 走看看