检查 AppDelegate 中的 method/property 是否存在
Check if method/property in AppDelegate exists
我试图找出 AppDelegate
是否包含某个 property/method。为此,我找到了 Check if a property exist in a class and How to check whether an object has certain method/property?,但 AppDelegate
似乎有所不同。
以下不会编译
if(AppDelegate.HasMethod("SomeMethod"))
因为
AppDelegate does not contain a defintion for HasMethod
.
我也尝试了其他变体,但我没有设法成功检查 method/property 是否存在。此外 respondsToSelector
似乎不适用于此处。 GetType()
也不适用于 AppDelegate
。
检查 AppDelegate
中的 property/method 是否存在的正确方法是什么?
编辑:
看来我需要一个 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
}
一些Class
if(AppDelegate.Self.RespondsToSelector(new Selector("YourMethod:")))
{
AppDelegate.Self.YourMethod (true);
}
您不需要使用 respondsToSelector
,如果您有AppDelegate
的实例。我的问题是如何确保 Self
在 AppDelegate
中实现?
是的,编译器会为我检查,但我只想在实现后才执行该方法。实施它不应该是必要的。它也应该在没有 YourMethod
.
的情况下工作
更新答案
我知道这仍然是 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
}
}
终于找到解决办法了。首先你需要两个扩展方法:
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。
但这并不是故事的结局。我的最终方法可以找到.
我试图找出 AppDelegate
是否包含某个 property/method。为此,我找到了 Check if a property exist in a class and How to check whether an object has certain method/property?,但 AppDelegate
似乎有所不同。
以下不会编译
if(AppDelegate.HasMethod("SomeMethod"))
因为
AppDelegate does not contain a defintion for
HasMethod
.
我也尝试了其他变体,但我没有设法成功检查 method/property 是否存在。此外 respondsToSelector
似乎不适用于此处。 GetType()
也不适用于 AppDelegate
。
检查 AppDelegate
中的 property/method 是否存在的正确方法是什么?
编辑:
看来我需要一个 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
}
一些Class
if(AppDelegate.Self.RespondsToSelector(new Selector("YourMethod:")))
{
AppDelegate.Self.YourMethod (true);
}
您不需要使用 respondsToSelector
,如果您有AppDelegate
的实例。我的问题是如何确保 Self
在 AppDelegate
中实现?
是的,编译器会为我检查,但我只想在实现后才执行该方法。实施它不应该是必要的。它也应该在没有 YourMethod
.
更新答案
我知道这仍然是 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
}
}
终于找到解决办法了。首先你需要两个扩展方法:
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。
但这并不是故事的结局。我的最终方法可以找到