lundi 23 novembre 2015

How to tell the compiler that a property is only accessed if it exists?

I can use HasProperty to check if a property exists. Only if the property exists a method should be executed.

How can the compiler successfully compile even if the property doesn't exist? E.g.

if (UIApplication.SharedApplication.Delegate.HasProperty("Instance"))
{
    AppDelegate customAppDelegate = UIApplication.SharedApplication.Delegate as AppDelegate;
    customAppDelegate.Instance.SomeMethod(true); // can't be compiled because Instance doesn't exist
}

The thing is this: First, I check if the propery exists. If yes I execute my method. So normally the code is never executed (except the property exists), but the compiler isn't able to differentiate this. It only checks if the property exists and doesn't take the if clause into account.

Is there a solution for this?





Aucun commentaire:

Enregistrer un commentaire