I try to find out if the AppDelegate
contains a certain property/method. For this I found Check if a property exist in a class and How to check whether an object has certain method/property?, but the AppDelegate
seems to be different.
The following will not compile
if(AppDelegate.HasMethod("SomeMethod"))
because
AppDelegate does not contain a defintion for
HasMethod
.
I also tried other variations but I didn't get it managed to successfully check if the method/property exists or not. Furthermore respondsToSelector
seems not to be applicable here. GetType()
is also not available for AppDelegate
.
What is the correct way for checking if a property/method in AppDelegate
exists?
Edit:
It seems that I need an instance of AppDelegate
to work with it. The question for me is how can I assure that this instance is available? E.g. through throwing an exception if it is not implemented?
Here is what you can do:
AppDelegate
public static new AppDelegate Self { get; private set; }
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
AppDelegate.Self = this;
return true;
}
[Export ("YourMethod:")]
public void YourMethod (bool setVisible){
// do something
}
Some Class
if(AppDelegate.Self.RespondsToSelector(new Selector("YourMethod:")))
{
AppDelegate.Self.YourMethod (true);
}
You don't need to use respondsToSelector
, you can use the other C#/.NET methods too (HasMethod
, HasProperty
from the linked threads) if you have the instance of AppDelegate
. The question for me is how can I assure that Self
is implemented in AppDelegate
?
Yes, the compiler checks that for me, but I want to execute the method only if it is implemented. It should not be a necessity to implement it. It should also work without YourMethod
.