【问题标题】:Check if method/property in AppDelegate exists检查 AppDelegate 中的方法/属性是否存在
【发布时间】:2016-02-25 12:11:15
【问题描述】:

我试图找出AppDelegate 是否包含某个属性/方法。为此我找到了Check if a property exist in a class 和How to check whether an object has certain method/property?,但AppDelegate 似乎有所不同。

以下不会编译

if(AppDelegate.HasMethod("SomeMethod"))

因为

AppDelegate 不包含HasMethod 的定义。

我也尝试了其他变体,但未能成功检查方法/属性是否存在。此外respondsToSelector 似乎不适用于这里。 GetType() 也不适用于AppDelegate。

检查AppDelegate 中的属性/方法是否存在的正确方法是什么?

编辑:

看来我需要一个AppDelegate 的实例来使用它。我的问题是如何确保此实例可用?例如。没有实现就抛出异常?

你可以这样做:

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
}

一些类

if(AppDelegate.Self.RespondsToSelector(new Selector("YourMethod:")))
{
    AppDelegate.Self.YourMethod (true);
}

您不需要使用respondsToSelector,如果您有AppDelegate 的实例,您也可以使用其他C#/.NET 方法(来自链接线程的HasMethod、HasProperty)。我的问题是如何确保Self 在AppDelegate 中实现?

是的,编译器会帮我检查,但我只想在方法被实现时才执行它。它不应该是实施它的必要条件。它也应该在没有YourMethod 的情况下工作。

【问题讨论】:

  • @try/catch 解决方案是否合适?这是一个有点粗略的方法,但应该可以。
  • @mylogon:这是可能的,但如果我访问一个可能不存在的属性,它会编译吗?

标签: c# ios reflection xamarin xamarin.ios


【解决方案1】:

终于找到了解决办法。首先你需要两个扩展方法:

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;
    } 
}

检查您使用的方法

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

检查您使用的属性

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

这个想法来自this thread。

但这并不是故事的结局。我的最终方法可以找到here。

【讨论】:

    【解决方案2】:

    更新答案

    我知道这仍然是 Obj-C,但您应该能够轻松获得 C# 等价物。

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

    祝你好运


    首先,

    #import "AppDelegate.h"
    

    您可以使用@try 块尝试以下方法尝试测试选择器。

        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
            }
    
        }
    

    【讨论】:

    • 感谢您的加入。我试图将您的代码翻译成 C#。在此我注意到我需要一个AppDelegate 的实际实例,您可以在其中发送performSelector 消息。我编辑了我的问题,向您展示我如何访问该实例。但我的主要问题仍然存在:如何确保 Self 属性在 AppDelegate 中实现?
    • 道歉。无论出于何种原因,我刚刚注意到“C#”。我不太熟悉。
    • 完全没问题。如果你能用 Objective-C/Swift 提供解决方案,我也很高兴,因为我大部分时间都可以翻译它们。您可以随时访问 Objective-C 中的AppDelegate 吗?或者你如何获得它的实例?
    • 啊。到时候我会改变我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    • 2021-04-22
    • 1970-01-01
    • 1970-01-01
    • 2018-02-28
    相关资源
    最近更新 更多