Domanda

I'm having a bit of an issue with a c# property throwing an exception when checking for null. I don't have access to the class that contains the property causing this issue, otherwise it would be an easy fix.

Basically, I have a view model that contains Imported_Class. The Imported_Class is populated with a method in a shared library. I ran into an issue when a property from Imported_Class throws an exception instead of returning null or a value.

It doesn't crash the app, I just can't check for null to prevent any possible errors.

I could assign that property to a local variable, wrapped in a try/catch to catch the error, but I find that too clunky just for one property.

An example of my issue:

public string getPropertyA
{
  get { return Imported_Class.PropertyA; /* throws exception */ }
}
È stato utile?

Soluzione

If Imported_Class doesn't provide a public way for you to test for the condition before getting the property, then I don't think there is any other choice than to wrap the call in a try/catch block.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top