简体   繁体   中英

Reflection PropertyInfo.GetValue

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.

You should be able to check for 'Null' directly as follows

if(propInfo.GetValue(this, null) != null) {
    }

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 :

A <column>_IsNull property for each selected input column. This property is also read-only or read/write depending on the Usage Type specified for the column.

PropertyInfo.GetValue(obj, null) is executing the property get method on the object obj . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM