Xamarin.iOS - System.Linq ToArray - 没有可访问的方法

Xamarin.iOS - System.Linq ToArray - no accessible method

我正在尝试将字典转换为 NSDictionary。

document = NSDictionary\<string, object\>(); // with some data of course

我这样做的方式是:

NSDictionary<NSString, NSObject> iosDoc = NSDictionary<NSString, NSObject>.FromObjectsAndKeys(
  document.Values.ToArray(),  
  document.Keys.ToArray()
);

我得到的错误如下:

Error CS1061: 'Dictionary<string, object>.ValueCollection' does not contain a definition for 'ToArray' and no accessible extension method 'ToArray' accepting a first argument of type 'Dictionary<string, object>.ValueCollection' could be found (are you missing a using directive or an assembly reference?) (CS1061)

Error CS1061: 'Dictionary<string, object>.KeyCollection' does not contain a definition for 'ToArray' and no accessible extension method 'ToArray' accepting a first argument of type 'Dictionary<string, object>.KeyCollection' could be found (are you missing a using directive or an assembly reference?) (CS1061)

这在以前的项目中有效,Visual Studio 识别此方法,但代码无法编译。

有什么想法吗?

如果您想从 Dictionary 转换为 NSDictionary,您可以尝试下面的代码。这对我来说很有用。我在 ios 15.4 和 xcode 13.3.

上进行了测试
 var document = new Dictionary<string, string>
            {
                {"A", "a"},
                {"B", "b"},
                {"C", "c"},
                {"D", "d"},
                {"E", "e"}
            };

        var iosDoc = NSDictionary.FromObjectsAndKeys(document.Values.ToArray(), document.Keys.ToArray());