From 8f67d632211bf1302df0aa964e013ffcc1945a87 Mon Sep 17 00:00:00 2001 From: Git History Editor Date: Thu, 25 May 2023 13:22:12 +0200 Subject: [PATCH] Use Enum.GetValues instead of Enum.GetValues in numerous places for .NET. NativeAOT warns about Enum.GetValues and suggests using Enum.GetValues instead, so do just that. --- msbuild/Xamarin.MacDev.Tasks/TargetArchitecture.cs | 4 ++++ src/Contacts/CNContact.cs | 4 ++++ src/PassKit/PKPaymentRequest.cs | 4 ++++ .../monotouch-test/AVFoundation/CaptureMetadataOutputTest.cs | 4 ++++ tests/monotouch-test/CoreImage/CIKernelTests.cs | 4 ++++ tests/monotouch-test/Foundation/CalendarTest.cs | 4 ++++ tests/monotouch-test/HealthKit/CategoryTypeIdentifierTest.cs | 4 ++++ tests/monotouch-test/HealthKit/QuantityTypeIdentifierTest.cs | 4 ++++ tests/monotouch-test/Metal/MTLDeviceTests.cs | 4 ++++ tests/monotouch-test/NaturalLanguage/EmbeddingTest.cs | 4 ++++ tests/monotouch-test/NaturalLanguage/NLTaggerTest.cs | 4 ++++ tests/monotouch-test/ObjCRuntime/RegistrarTest.cs | 4 ++++ tests/monotouch-test/SceneKit/SCNGeometrySource.cs | 4 ++++ tests/xharness/TestLabel.cs | 4 ++++ tests/xharness/Xharness.Tests/Jenkins/TestSelectionTests.cs | 4 ++++ tools/common/Optimizations.cs | 4 ++++ 16 files changed, 64 insertions(+) diff --git a/msbuild/Xamarin.MacDev.Tasks/TargetArchitecture.cs b/msbuild/Xamarin.MacDev.Tasks/TargetArchitecture.cs index 707efaa33a4c..ceb5d55de1b5 100644 --- a/msbuild/Xamarin.MacDev.Tasks/TargetArchitecture.cs +++ b/msbuild/Xamarin.MacDev.Tasks/TargetArchitecture.cs @@ -28,7 +28,11 @@ public static IList ToArray (this TargetArchitecture self) var rv = new List (); +#if NET + var simpleValues = new List (Enum.GetValues ()); +#else var simpleValues = new List ((TargetArchitecture []) Enum.GetValues (typeof (TargetArchitecture))); +#endif simpleValues.Remove (TargetArchitecture.Default); simpleValues.Remove (TargetArchitecture.ARMv6_ARMv7); diff --git a/src/Contacts/CNContact.cs b/src/Contacts/CNContact.cs index ddc010657e66..6e024ed915e7 100644 --- a/src/Contacts/CNContact.cs +++ b/src/Contacts/CNContact.cs @@ -93,7 +93,11 @@ public bool AreKeysAvailable (T [] keyDescriptors) public bool AreKeysAvailable (CNContactOptions options) { using (var array = new NSMutableArray ()) { +#if NET + foreach (var value in Enum.GetValues ()) { +#else foreach (CNContactOptions value in Enum.GetValues (typeof (CNContactOptions))) { +#endif if ((options & value) != CNContactOptions.None) array.Add (ContactOptionsToNSString (value)); } diff --git a/src/PassKit/PKPaymentRequest.cs b/src/PassKit/PKPaymentRequest.cs index b5f48d7caab5..4717be7e9077 100644 --- a/src/PassKit/PKPaymentRequest.cs +++ b/src/PassKit/PKPaymentRequest.cs @@ -29,7 +29,11 @@ static public NSSet GetSet (PKContactFields values) if (values == PKContactFields.None) return set; +#if NET + foreach (var value in Enum.GetValues ()) { +#else foreach (PKContactFields value in Enum.GetValues (typeof (PKContactFields))) { +#endif if (values.HasFlag (value)) { var constant = value.GetConstant (); // None does not have an associated native value and Contains would throw an ANE diff --git a/tests/monotouch-test/AVFoundation/CaptureMetadataOutputTest.cs b/tests/monotouch-test/AVFoundation/CaptureMetadataOutputTest.cs index 1edf388b8178..a411cb9b097c 100644 --- a/tests/monotouch-test/AVFoundation/CaptureMetadataOutputTest.cs +++ b/tests/monotouch-test/AVFoundation/CaptureMetadataOutputTest.cs @@ -109,7 +109,11 @@ public void MetadataObjectTypesTest () captureSession.AddOutput (metadataOutput); AVMetadataObjectType all = AVMetadataObjectType.None; +#if NET + foreach (var val in Enum.GetValues ()) { +#else foreach (AVMetadataObjectType val in Enum.GetValues (typeof (AVMetadataObjectType))) { +#endif switch (val) { case AVMetadataObjectType.CatBody: case AVMetadataObjectType.DogBody: diff --git a/tests/monotouch-test/CoreImage/CIKernelTests.cs b/tests/monotouch-test/CoreImage/CIKernelTests.cs index 55ceb296afaa..de3e9a057889 100644 --- a/tests/monotouch-test/CoreImage/CIKernelTests.cs +++ b/tests/monotouch-test/CoreImage/CIKernelTests.cs @@ -137,7 +137,11 @@ public void CIKernel_BasicTest () #endif +#if NET + foreach (var type in Enum.GetValues ()) { +#else foreach (CustomerFilterType type in Enum.GetValues (typeof (CustomerFilterType))) { +#endif MyCustomFilter filter = new MyCustomFilter (type); filter.MyImage = ciImg; diff --git a/tests/monotouch-test/Foundation/CalendarTest.cs b/tests/monotouch-test/Foundation/CalendarTest.cs index 8cdc414dc13b..4a8906bc07ce 100644 --- a/tests/monotouch-test/Foundation/CalendarTest.cs +++ b/tests/monotouch-test/Foundation/CalendarTest.cs @@ -99,7 +99,11 @@ public void GetAllCalendarIdentifiers () { RequiresIos8 (); +#if NET + foreach (var t in Enum.GetValues ()) { +#else foreach (NSCalendarType t in Enum.GetValues (typeof (NSCalendarType))) { +#endif switch (t) { case NSCalendarType.IslamicTabular: case NSCalendarType.IslamicUmmAlQura: diff --git a/tests/monotouch-test/HealthKit/CategoryTypeIdentifierTest.cs b/tests/monotouch-test/HealthKit/CategoryTypeIdentifierTest.cs index f3624e823710..87a8a5b4a5c6 100644 --- a/tests/monotouch-test/HealthKit/CategoryTypeIdentifierTest.cs +++ b/tests/monotouch-test/HealthKit/CategoryTypeIdentifierTest.cs @@ -38,7 +38,11 @@ public void EnumValues_22351 () var failures = new List (); +#if NET + foreach (var value in Enum.GetValues ()) { +#else foreach (HKCategoryTypeIdentifier value in Enum.GetValues (typeof (HKCategoryTypeIdentifier))) { +#endif switch (value) { case HKCategoryTypeIdentifier.SleepAnalysis: diff --git a/tests/monotouch-test/HealthKit/QuantityTypeIdentifierTest.cs b/tests/monotouch-test/HealthKit/QuantityTypeIdentifierTest.cs index e0ca8eae33a6..fdff8a53edd4 100644 --- a/tests/monotouch-test/HealthKit/QuantityTypeIdentifierTest.cs +++ b/tests/monotouch-test/HealthKit/QuantityTypeIdentifierTest.cs @@ -38,7 +38,11 @@ public void EnumValues_22351 () var failures = new List (); +#if NET + foreach (var value in Enum.GetValues ()) { +#else foreach (HKQuantityTypeIdentifier value in Enum.GetValues (typeof (HKQuantityTypeIdentifier))) { +#endif // we need to have version checks for anything added after iOS 8.0 switch (value) { diff --git a/tests/monotouch-test/Metal/MTLDeviceTests.cs b/tests/monotouch-test/Metal/MTLDeviceTests.cs index dc165e38ad3a..f23500d9e365 100644 --- a/tests/monotouch-test/Metal/MTLDeviceTests.cs +++ b/tests/monotouch-test/Metal/MTLDeviceTests.cs @@ -100,7 +100,11 @@ public void ReturnReleaseTest () Console.WriteLine ($"This device supports feature set: {fs}: {device.SupportsFeatureSet (fs)}"); } if (TestRuntime.CheckXcodeVersion (11, 0)) { +#if NET + foreach (var gf in Enum.GetValues ()) { +#else foreach (MTLGpuFamily gf in Enum.GetValues (typeof (MTLGpuFamily))) { +#endif Console.WriteLine ($"This device supports Gpu family: {gf}: {device.SupportsFamily (gf)}"); } } diff --git a/tests/monotouch-test/NaturalLanguage/EmbeddingTest.cs b/tests/monotouch-test/NaturalLanguage/EmbeddingTest.cs index ea5c925b1cb0..3941a9be58d0 100644 --- a/tests/monotouch-test/NaturalLanguage/EmbeddingTest.cs +++ b/tests/monotouch-test/NaturalLanguage/EmbeddingTest.cs @@ -22,7 +22,11 @@ public void Vector () { TestRuntime.AssertXcodeVersion (11, 0); +#if NET + foreach (var v in Enum.GetValues ()) { +#else foreach (NLLanguage v in Enum.GetValues (typeof (NLLanguage))) { +#endif if (v == NLLanguage.Unevaluated) continue; // this is not a value provided by Apple. diff --git a/tests/monotouch-test/NaturalLanguage/NLTaggerTest.cs b/tests/monotouch-test/NaturalLanguage/NLTaggerTest.cs index b5cea2104f8c..b023ee4d9366 100644 --- a/tests/monotouch-test/NaturalLanguage/NLTaggerTest.cs +++ b/tests/monotouch-test/NaturalLanguage/NLTaggerTest.cs @@ -54,7 +54,11 @@ public void GetModels () { TestRuntime.AssertXcodeVersion (11, 0); using (var tagger = new NLTagger (NLTagScheme.LexicalClass) { String = Text }) { +#if NET foreach (var scheme in typeof (NLTagScheme).GetEnumValues ()) { +#else + foreach (NLTagScheme scheme in Enum.GetValues (typeof (NLTagScheme))) { +#endif var constant = ((NLTagScheme) scheme).GetConstant (); if (constant is null) continue; // can vary by SDK version diff --git a/tests/monotouch-test/ObjCRuntime/RegistrarTest.cs b/tests/monotouch-test/ObjCRuntime/RegistrarTest.cs index 24f1324a1e91..ea2afbbac599 100644 --- a/tests/monotouch-test/ObjCRuntime/RegistrarTest.cs +++ b/tests/monotouch-test/ObjCRuntime/RegistrarTest.cs @@ -1042,7 +1042,11 @@ public virtual void TestNativeEnum1 (NSWritingDirection twd, int a, long b) [Export ("testNativeEnum3:a:b:")] public virtual void TestNativeEnum1 (UITextWritingDirection twd, int a, long b) { +#if NET + Assert.That (Enum.GetValues (), Contains.Item (twd), "TestNativeEnum3"); +#else Assert.That (Enum.GetValues (typeof (UITextWritingDirection)), Contains.Item (twd), "TestNativeEnum3"); +#endif Assert.AreEqual (31415, a, "TestNativeEnum3 a"); Assert.AreEqual (3141592, b, "TestNativeEnum3 b"); } diff --git a/tests/monotouch-test/SceneKit/SCNGeometrySource.cs b/tests/monotouch-test/SceneKit/SCNGeometrySource.cs index f410f25bff4f..0865160001ea 100644 --- a/tests/monotouch-test/SceneKit/SCNGeometrySource.cs +++ b/tests/monotouch-test/SceneKit/SCNGeometrySource.cs @@ -51,7 +51,11 @@ public void SCNGeometrySource_FromDataTest () Asserts.EnsureMountainLion (); #pragma warning disable 0219 SCNGeometrySource d = SCNGeometrySource.FromData (new NSData (), SCNGeometrySourceSemantic.Color, 1, false, 1, 1, 1, 1); +#if NET + foreach (var s in Enum.GetValues ()) { +#else foreach (SCNGeometrySourceSemantics s in Enum.GetValues (typeof (SCNGeometrySourceSemantics))) { +#endif if (!isValidEnumForPlatform (s)) continue; d = SCNGeometrySource.FromData (new NSData (), s, 1, false, 1, 1, 1, 1); diff --git a/tests/xharness/TestLabel.cs b/tests/xharness/TestLabel.cs index 99d8847c9407..f48959874c22 100644 --- a/tests/xharness/TestLabel.cs +++ b/tests/xharness/TestLabel.cs @@ -126,7 +126,11 @@ static string GetLabel (this T self) where T : Enum public static bool TryGetLabel (this string self, out T? label) where T : Enum { +#if NET + foreach (var obj in Enum.GetValues ()) { +#else foreach (var obj in Enum.GetValues (typeof (T))) { +#endif if (obj is T value && value.GetLabel () == self) { label = value; return true; diff --git a/tests/xharness/Xharness.Tests/Jenkins/TestSelectionTests.cs b/tests/xharness/Xharness.Tests/Jenkins/TestSelectionTests.cs index 5adfdc8bef06..7a81b65073d3 100644 --- a/tests/xharness/Xharness.Tests/Jenkins/TestSelectionTests.cs +++ b/tests/xharness/Xharness.Tests/Jenkins/TestSelectionTests.cs @@ -115,7 +115,11 @@ public void EnableSeveralTests () selection.SetEnabled (TestLabel.All, false); selection.SetEnabled (TestLabel.Bcl, true); selection.SetEnabled (TestLabel.Monotouch, true); +#if NET + foreach (var obj in Enum.GetValues ()) { +#else foreach (var obj in Enum.GetValues (typeof (TestLabel))) { +#endif if (obj is TestLabel label) { switch (label) { case TestLabel.All: diff --git a/tools/common/Optimizations.cs b/tools/common/Optimizations.cs index 361ea1267def..ca6a7af6119d 100644 --- a/tools/common/Optimizations.cs +++ b/tools/common/Optimizations.cs @@ -377,7 +377,11 @@ void ParseOption (ApplePlatform platform, string option, List values [i] = enabled; } if (!found) +#if NET + messages.Add (ErrorHelper.CreateWarning (132, Errors.MX0132, opt, string.Join (", ", Enum.GetValues ().Where (o => Array.IndexOf (valid_platforms [(int) o], platform) >= 0).Select (o => opt_names [(int) o])))); +#else messages.Add (ErrorHelper.CreateWarning (132, Errors.MX0132, opt, string.Join (", ", Enum.GetValues (typeof (Opt)).Cast ().Where (o => Array.IndexOf (valid_platforms [(int) o], platform) >= 0).Select (o => opt_names [(int) o])))); +#endif } }