diff --git a/src/ObjCRuntime/PlatformAvailability2.cs b/src/ObjCRuntime/PlatformAvailability2.cs index 04477a67d381..2a2f08a266a7 100644 --- a/src/ObjCRuntime/PlatformAvailability2.cs +++ b/src/ObjCRuntime/PlatformAvailability2.cs @@ -92,6 +92,76 @@ internal AvailabilityBaseAttribute ( public override string ToString () { var builder = new StringBuilder (); +#if NET + switch (AvailabilityKind) { + case AvailabilityKind.Introduced: + builder.Append ("[SupportedOSPlatform (\""); + break; + case AvailabilityKind.Obsoleted: + switch (Platform) { + case PlatformName.iOS: + builder.AppendLine ("#if __IOS__"); + break; + case PlatformName.TvOS: + builder.AppendLine ("#if __TVOS__"); + break; + case PlatformName.WatchOS: + builder.AppendLine ("#if __WATCHOS__"); + break; + case PlatformName.MacOSX: + builder.AppendLine ("#if __MACOS__"); + break; + case PlatformName.MacCatalyst: + builder.AppendLine ("#if __MACCATALYST__"); + break; + } + builder.Append ("[Obsolete (\"Starting with "); + break; + case AvailabilityKind.Deprecated: + case AvailabilityKind.Unavailable: + builder.Append ("[UnsupportedOSPlatform (\""); + break; + } + + switch (Platform) { + case PlatformName.iOS: + builder.Append ("ios"); + break; + case PlatformName.TvOS: + builder.Append ("tvos"); + break; + case PlatformName.WatchOS: + builder.Append ("watchos"); + break; + case PlatformName.MacOSX: + builder.Append ("macos"); // no 'x' + break; + case PlatformName.MacCatalyst: + builder.Append ("maccatalyst"); + break; + } + + if (Version != null) { + builder.Append (Version.Major).Append ('.').Append (Version.Minor); + if (Version.Build >= 0) + builder.Append ('.').Append (Version.Build); + } + + switch (AvailabilityKind) { + case AvailabilityKind.Obsoleted: + if (!String.IsNullOrEmpty (Message)) + builder.Append (' ').Append (Message); + // TODO add a URL (wiki?) and DiagnosticId (one per platform?) for documentation + builder.AppendLine ("\", DiagnosticId = \"BI1234\", UrlFormat = \"https://github.com/xamarin/xamarin-macios/wiki/Obsolete\")]"); + builder.Append ("#endif"); + break; + case AvailabilityKind.Introduced: + case AvailabilityKind.Deprecated: + case AvailabilityKind.Unavailable: + builder.Append ("\")]"); + break; + } +#else builder.AppendFormat ("[{0} ({1}.{2}", AvailabilityKind, nameof (PlatformName), Platform); if (Version != null) { @@ -107,6 +177,7 @@ public override string ToString () builder.AppendFormat (", message: \"{0}\"", Message.Replace ("\"", "\"\"")); builder.Append (")]"); +#endif return builder.ToString (); } } diff --git a/src/generator.cs b/src/generator.cs index c5b4c7ed50ff..81eb69fc6723 100644 --- a/src/generator.cs +++ b/src/generator.cs @@ -728,6 +728,9 @@ public NamespaceManager (BindingTouch binding_touch, string customObjCRuntimeNS, ImplicitNamespaces.Add ("System.Diagnostics"); ImplicitNamespaces.Add ("System.Diagnostics.CodeAnalysis"); ImplicitNamespaces.Add ("System.ComponentModel"); +#if NET + ImplicitNamespaces.Add ("System.Runtime.Versioning"); +#endif ImplicitNamespaces.Add ("System.Threading.Tasks"); ImplicitNamespaces.Add ("CoreFoundation"); ImplicitNamespaces.Add ("Foundation"); @@ -5682,7 +5685,13 @@ void GenerateProtocolTypes (Type type, string class_visibility, string TypeName, var optionalInstanceProperties = allProtocolProperties.Where ((v) => !IsRequired (v) && !AttributeManager.HasAttribute (v)); var requiredInstanceAsyncMethods = requiredInstanceMethods.Where (m => AttributeManager.HasAttribute (m)).ToList (); +#if NET + // [SupportedOSPlatform] cannot be used on interface - https://github.com/dotnet/runtime/issues/47599 + // instead we'll generate them on the members + PrintAttributes (type, platform:false, preserve:true, advice:true); +#else PrintAttributes (type, platform:true, preserve:true, advice:true); +#endif print ("[Protocol (Name = \"{1}\", WrapperType = typeof ({0}Wrapper){2}{3})]", TypeName, protocol_name, @@ -5822,6 +5831,10 @@ void GenerateProtocolTypes (Type type, string class_visibility, string TypeName, var mod = string.Empty; PrintMethodAttributes (minfo); +#if NET + // TODO - generate them only if they do not exists on the member + PrintPlatformAttributes (type); +#endif PrintPlatformAttributes (mi); print_generated_code (); PrintDelegateProxy (minfo);