This question already has an answer here:
The application I'm working on has a piece of code which fails occasionally when object's property's value is null.
I need to check for the value before using this code:
foreach(PropertyInfo pInfo in this.GetType().GetProperties())
{
int index = pInfo.Name.IndexOfAny(new char[] {'2'});
if( index > -1 && pInfo.PropertyType.ToString() != "System.Boolean" && pInfo.Name != "Bank2AccountType"
&& ! pInfo.PropertyType.IsEnum )
{
// Get the value of the current property
string propertyValue = pInfo.GetValue(this, null).ToString();
if( propertyValue != string.Empty )
{
// There is a value in the current property, the bank 2 is not empty
isEmpty = false;
// There is no need to do further checks, break out of the loop and return
break;
}
}
propertyIndex++;
}
This is where it fails:
string propertyValue = pInfo.GetValue(this, null).ToString();
I assume, that this
is the object which has 'pInfo' property.
I need to check for null
before assigning it to "propertyValue" variable
What is the right way to do so?
Aucun commentaire:
Enregistrer un commentaire