反射 PropertyInfo.GetValue
我是在 C# 中使用反射的新手。非常感谢任何帮助。
PropertyInfo.GetValue(obj, null) 给我一个对象值。
如果数据库中该列的值为空,我会得到一个 Null 异常:
System.Reflection.TargetInitationException:调用目标已引发异常。 Microsoft.SqlServer.Dts.Pipeline.ColumnIsNullException:该列具有空值。
遇到这种情况该如何处理呢?我应该循环遍历所有列并保留具有空值的列。
I am new to using reflection in C#. Any help is much appreciated.
PropertyInfo.GetValue(obj, null) gives me a object value.
If the value of the column is null in the database, I get a Null exception:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
Microsoft.SqlServer.Dts.Pipeline.ColumnIsNullException: The column has a null value.
How to handle this situation? I should loop through all the columns and leave the columns which have a null value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以直接检查“Null”,如下所示
You can check for 'Null' directly as follows
该属性的 getter 正在抛出异常。它试图告诉您该财产没有价值。
您应该能够首先检查
PropertyName_IsNull
(其中 PropertyName 是属性的名称)来检查属性是否为 null。如果为空,请进行适当处理,否则使用您已经编写的代码。来自 MSDN:
The getter of that property is throwing an excepting. It's trying to tell you that the property doesn't have a value.
You should be able to check
PropertyName_IsNull
(Where PropertyName is the name of the property) to check if the property is null first. If it is null, handle appropriately, otherwise use the code you've already written.From MSDN:
PropertyInfo.GetValue(obj, null)
正在对象obj
上执行属性 get 方法。该 get 方法中抛出异常。您需要查看正在调用的属性 get 方法并确定何时/为何引发异常。PropertyInfo.GetValue(obj, null)
is executing the property get method on the objectobj
. The exception is being thrown in that get method. You need to look at the property get method you're invoking and determine when/why an exception is being thrown.