Skip to content

Commit a9a1b98

Browse files
rolfbjarneCopilot
andauthored
[ObjCRuntime] Enable nullability and clean up CategoryAttribute. (#24744)
This is file 6 of 7 files with nullability disabled in ObjCRuntime. * Enable nullability (#nullable enable). * Add nullability-aware API updates for category name metadata. * Inline XML documentation from CategoryAttribute.xml into source and remove the include file. * Improve XML documentation formatting, grammar, and cref usage. Contributes towards #17285. --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent d0d4791 commit a9a1b98

File tree

2 files changed

+57
-88
lines changed

2 files changed

+57
-88
lines changed

docs/api/ObjCRuntime/CategoryAttribute.xml

Lines changed: 0 additions & 68 deletions
This file was deleted.
Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,76 @@
11
//
2-
// CategoryAttribyute.cs:
2+
// CategoryAttribute.cs:
33
//
44
// Authors:
55
// Rolf Bjarne Kvinge <rolf@xamarin.com>
66
//
77
// Copyright 2015 Xamarin Inc.
88

9-
// Disable until we get around to enable + fix any issues.
10-
#nullable disable
9+
#nullable enable
1110

1211
namespace ObjCRuntime {
13-
/// <include file="../../docs/api/ObjCRuntime/CategoryAttribute.xml" path="/Documentation/Docs[@DocId='T:ObjCRuntime.CategoryAttribute']/*" />
12+
/// <summary>Attribute used to flag a class as a category that extends another type.</summary>
13+
/// <remarks>
14+
/// <para>This attribute is applied to static user-code classes and surfaces all exported methods and properties (those with the <see cref="Foundation.ExportAttribute"/>) to the provided system type.</para>
15+
/// <para>This allows new methods to be introduced or implemented for all types in the class. For example, this can be used to provide a global implementation of some method across all your types surfaced to Objective-C.</para>
16+
/// <para>All managed extension methods must be static, but it is possible to create Objective-C instance methods using the standard syntax for extension methods in C#:</para>
17+
/// <example>
18+
/// <code lang="csharp"><![CDATA[
19+
/// // Make "shouldAutoRotate" return true for all UIViewControllers in the application
20+
///
21+
/// [Category (typeof (UIViewController))]
22+
/// static class MyViewControllerMethods {
23+
/// [Export ("shouldAutorotate")]
24+
/// static bool ShouldAutoRotate (this UIViewController self)
25+
/// {
26+
/// return true;
27+
/// }
28+
///
29+
/// [Export ("supportedInterfaceOrientations")]
30+
/// static UIInterfaceOrientationMask SupportedRotations (this UIViewController self)
31+
/// {
32+
/// return UIInterfaceOrientationMask.All;
33+
/// }
34+
/// }
35+
/// ]]></code>
36+
/// </example>
37+
/// <example>
38+
/// <code lang="csharp"><![CDATA[
39+
/// // This example adds a native toUpper instance method to the NSString class,
40+
/// // which can be invoked from Objective-C.
41+
///
42+
/// [Category (typeof (NSString))]
43+
/// public static class MyStringCategory
44+
/// {
45+
/// [Export ("toUpper")]
46+
/// static string ToUpper (this NSString self)
47+
/// {
48+
/// return self.ToString ().ToUpper ();
49+
/// }
50+
/// }
51+
/// ]]></code>
52+
/// </example>
53+
/// <para>If the managed class is not referenced by other managed code (and is only called from Objective-C), the managed linker may remove it. This can be avoided by either adding a <see cref="Foundation.PreserveAttribute"/> attribute to the class, or by creating a custom linker definition file.</para>
54+
/// <para>See <see href="https://docs.microsoft.com/xamarin/cross-platform/deploy-test/linker">Custom Linker Configuration</see> for more information.</para>
55+
/// </remarks>
1456
[AttributeUsage (AttributeTargets.Class)]
1557
public class CategoryAttribute : Attribute {
16-
/// <param name="type">The Objective-C type to extend. This must be a subclass of NSObject (or NSObject itself).</param>
17-
/// <summary>The type that this category extends.</summary>
18-
/// <remarks>
19-
/// </remarks>
58+
/// <summary>Initializes a new instance of the <see cref="CategoryAttribute"/> class.</summary>
59+
/// <param name="type">The Objective-C type to extend. This must be a subclass of <see cref="Foundation.NSObject"/> (or <see cref="Foundation.NSObject"/> itself).</param>
2060
public CategoryAttribute (Type type)
2161
{
2262
Type = type;
2363
}
2464

25-
/// <summary>The type that this category extends.</summary>
26-
/// <value>
27-
/// </value>
28-
/// <remarks>
29-
/// </remarks>
65+
/// <summary>Gets or sets the type that this category extends.</summary>
66+
/// <value>The type this category extends.</value>
3067
public Type Type { get; set; }
31-
/// <summary>The name of the category.</summary>
32-
/// <value>
33-
/// </value>
34-
/// <remarks>
35-
/// <para>This must be a valid Objective-C type name, but is otherwise unused.</para>
36-
/// </remarks>
37-
public string Name { get; set; }
68+
69+
/// <summary>Gets or sets the name of the category.</summary>
70+
/// <value>The category name, or <see langword="null"/> to use the managed type name.</value>
71+
/// <remarks>
72+
/// <para>This must be a valid Objective-C type name, but is otherwise unused.</para>
73+
/// </remarks>
74+
public string? Name { get; set; }
3875
}
3976
}

0 commit comments

Comments
 (0)