0

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.

Community
  • 1
  • 1
testing
  • 19,681
  • 50
  • 236
  • 417

2 Answers2

1

Updated answer

I know that this is still Obj-C, but you should be able to easily get the C# equivalent.

#import "AppDelegate.h"

SEL selector = NSSelectorFromString(@"possibleMethod");
AppDelegate * appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
if([appDelegate respondsToSelector:selector]){
    [appDelegate selector];
}

Good luck


Firstly,

#import "AppDelegate.h"

You could try the below method using a @try block try test the selector.

    BOOL methodExists = YES;
    SEL yourVariable = NSSelectorFromString(@"possibleMethod");
    AppDelegate * ad = [[AppDelegate alloc]init];

    @try {
        [ad performSelector:yourVariable withObject:nil afterDelay:0.0];
    }
    @catch (NSException *exception) {
        methodExists = NO;
    }
    @finally {

        if (methodExists) {
            //Call method from selector
        }

    }
mylogon
  • 2,772
  • 2
  • 28
  • 42
  • Thanks for your addition. I tried to translate your code into C#. Hereby I noticed that I need an actual instance of `AppDelegate`, where you can send the `performSelector` message. I edited my question to show you how I get access to the instance. But my main problem is still present: How can I assure that the property `Self` is implemented in the `AppDelegate`? – testing Nov 23 '15 at 12:35
  • Apologies. For whatever reason I just noticed 'C#'. I'm not as familiar with that. – mylogon Nov 23 '15 at 12:42
  • No problem at all. I'm also happy if you can provide solutions in Objective-C/Swift, because I can translate them most of the time. Are you able to access the `AppDelegate` in Objective-C at any time? Or how do you get the instance of it? – testing Nov 23 '15 at 12:44
  • Ah. I will change my answer to suit then. – mylogon Nov 23 '15 at 12:47
1

Finally I found a solution. First you need two extension methods:

public static class GeneralExtensions
{
    public static bool HasProperty(this object obj, string propertyName)
    {
        return obj.GetType().GetProperty(propertyName) != null;
    }

    public static bool HasMethod(this object objectToCheck, string methodName)
    {
        var type = objectToCheck.GetType();
        return type.GetMethod(methodName) != null;
    } 
}

To check for a method you use

if (UIApplication.SharedApplication.Delegate.HasMethod("YourMethod")){
    // do something
}

To check for a property you use

if (UIApplication.SharedApplication.Delegate.HasProperty("Instance"))
    // do something
}

The idea came from this thread.

But this is not the end of the story. My final approach can be found here.

Community
  • 1
  • 1
testing
  • 19,681
  • 50
  • 236
  • 417