Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 691
user922330 Avatar asked Sep 07 '11 16:09

user922330


People also ask

What is GetValue in C#?

GetValue() Method in C# is used to gets the value of the specified element in the current Array.

What is PropertyInfo?

The PropertyInfo class discovers the attributes of a property and provides access to property metadata. The PropertyInfo class is very similar to the FieldInfo class and also contains the ability to set the value of the property on an instance.


2 Answers

You can check for 'Null' directly as follows

if(propInfo.GetValue(this, null) != null) {
}
like image 73
Maneksh Veetinal Avatar answered Sep 30 '22 08:09

Maneksh Veetinal


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.

like image 21
vcsjones Avatar answered Sep 30 '22 09:09

vcsjones