Xamarin.ios 如何在Xamarin中获得iOS设备CPU架构?

Xamarin.ios 如何在Xamarin中获得iOS设备CPU架构?,xamarin.ios,Xamarin.ios,在我的Xamarin iOS应用程序中,我可以从UIKit.UIDevice.CurrentDevice实例中获得许多设备特征,如型号、系统名称等。但是,我没有看到在类上获得CPU体系结构(x86、arm等)的任何方法 显示了使用Objective C获取此信息的方法。我想知道是否有方法使用任何预定义的Xamarin类获取C#中的CPU信息。在iOS项目上使用此类创建新文件: public static class DeviceInfo { public const string Har

在我的Xamarin iOS应用程序中,我可以从
UIKit.UIDevice.CurrentDevice
实例中获得许多设备特征,如型号、系统名称等。但是,我没有看到在类上获得CPU体系结构(x86、arm等)的任何方法


显示了使用Objective C获取此信息的方法。我想知道是否有方法使用任何预定义的Xamarin类获取C#中的CPU信息。

在iOS项目上使用此类创建新文件:

public static class DeviceInfo
{
    public const string HardwareSysCtlName = "hw.machine";

    public static string HardwareArch { get; private set; }

    [DllImport(ObjCRuntime.Constants.SystemLibrary)]
    static internal extern int sysctlbyname([MarshalAs(UnmanagedType.LPStr)] string property, IntPtr output, IntPtr oldLen, IntPtr newp, uint newlen);

    static DeviceInfo()
    {
        var pLen = Marshal.AllocHGlobal(sizeof(int));
        sysctlbyname(HardwareSysCtlName, IntPtr.Zero, pLen, IntPtr.Zero, 0);

        var length = Marshal.ReadInt32(pLen);

        var pStr = Marshal.AllocHGlobal(length);
        sysctlbyname(HardwareSysCtlName, pStr, pLen, IntPtr.Zero, 0);

        HardwareArch = Marshal.PtrToStringAnsi(pStr);
    }
}

在iOS项目上使用此类创建新文件:

public static class DeviceInfo
{
    public const string HardwareSysCtlName = "hw.machine";

    public static string HardwareArch { get; private set; }

    [DllImport(ObjCRuntime.Constants.SystemLibrary)]
    static internal extern int sysctlbyname([MarshalAs(UnmanagedType.LPStr)] string property, IntPtr output, IntPtr oldLen, IntPtr newp, uint newlen);

    static DeviceInfo()
    {
        var pLen = Marshal.AllocHGlobal(sizeof(int));
        sysctlbyname(HardwareSysCtlName, IntPtr.Zero, pLen, IntPtr.Zero, 0);

        var length = Marshal.ReadInt32(pLen);

        var pStr = Marshal.AllocHGlobal(length);
        sysctlbyname(HardwareSysCtlName, pStr, pLen, IntPtr.Zero, 0);

        HardwareArch = Marshal.PtrToStringAnsi(pStr);
    }
}

今天,我发现这段代码不仅适用于Windows(UWP),也适用于使用Xamarin的iOS:

System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture.ToString();
在iOS模拟器中,运行时信息包含更多信息,如OSArchitecture,但在真正的iOS设备中,只有ProcessArchitecture可用


我使用了所有可用的最新软件:Xcode 12.4、Visual Studio for Mac 8.9(1651)等。

今天,我发现该代码不仅适用于Windows(UWP),也适用于使用Xamarin的iOS:

System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture.ToString();
在iOS模拟器中,运行时信息包含更多信息,如OSArchitecture,但在真正的iOS设备中,只有ProcessArchitecture可用


我使用了所有可用的最新软件:Xcode 12.4、Visual Studio for Mac 8.9(1651)等。

非常感谢您的帮助。虽然“hw.machine”不是我需要的,但您的代码确实让我知道了我需要做什么。非常感谢您的帮助。虽然“hw.machine”不是我需要的,但您的代码确实让我知道了我需要做什么。