我需要检测我的代码是否在Xamarin.iOS上运行,因此,我编写了下一个代码:
bool isIOS()
{
try
{
var assmbl = Assembly.Load("Xamarin.iOS.dll");
if (assmbl == null) return false;
var classType = assmbl.GetType("UIKit.UIDevice");
if (classType == null) return false; // HERE IS THE PROBLEM !!!
var prop = classType.GetProperty("CurrentDevice");
if (prop == null) return false;
var currDevice = prop.GetValue(null);
if (currDevice == null) return false;
var sType = currDevice.GetType();
prop = sType.GetProperty("SystemName");
if (prop == null) return false;
var name = prop.GetValue(currDevice) as string;
return (name == "iOS");
}
catch
{
return false;
}
}
问题是assmbl.GetType(“ UIKit.UIDevice”)在模拟器上返回正确的Type,并且此代码按预期工作,但是在实际设备上,它返回null。
此外,如果我调用assmbl.GetTypes(),则该方法在模拟器上返回大约1000种类型,在设备上返回大约100种类型。
UIDevice.CurrentDevice.SystemName在模拟器和设备上均可正常运行,但我不能直接使用它,因为代码应在Xamarin和非Xamarin应用程序中正常工作。
这种行为的原因可能是什么?