使用System.Reflection.PropertyInfo获取内部对象信息(值) [英] get the Inner object information (values) by using System.Reflection.PropertyInfo
本文介绍了使用System.Reflection.PropertyInfo获取内部对象信息(值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
// Object1(它有多个内部对象)
i在尝试读取内部对象时遇到异常
< pre lang =cs> public static void WritePDF< T>(T Object1, int RrcordID)
{
类型type = typeof运算跨度>(T);
foreach (System.Reflection.PropertyInfo属性 in type.GetProperties())
{
svalue = type.GetProperty(property.Name).GetValue(Object1, null )。ToString();
}
}
在此之前我需要检查它并获取内部对象值,如何?解决方案如果不确切知道你遇到了什么异常,很难确定出现了什么问题。
但这一行
svalue = type.GetProperty(property.Name).GetValue(Object1,null).ToString();
如果出现以下情况,至少会失败:< br $> b $ b
- 1。该属性的值为
null
,因为对ToString
的调用将导致NullReferenceException
. - 2。该属性是一个索引器属性(它可能会抛出异常)
希望这会有所帮助,
Fredrik
// Object1 (it has multiple inner object)
i was getting exception when ever try to read the inner object at
public static void WritePDF<T>(T Object1 ,int RrcordID)
{
Type type = typeof(T);
foreach (System.Reflection.PropertyInfo property in type.GetProperties())
{
svalue = type.GetProperty(property.Name).GetValue(Object1, null).ToString();
}
}
before this i need to check it and get the inner object value ,How ?
解决方案
Without knowing exactly what exception you're getting it's hard to say for sure what's going wrong.
But this line
svalue = type.GetProperty(property.Name).GetValue(Object1, null).ToString();
Will at least fail if:
- 1. The value of the property is
null
as the call toToString
will then cause aNullReferenceException
.- 2. The property is an indexer property (it's likely to throw an exception)
Hope this helps,
Fredrik
这篇关于使用System.Reflection.PropertyInfo获取内部对象信息(值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文