-
Notifications
You must be signed in to change notification settings - Fork 565
[ObjCRuntime] Enable nullability and clean up CategoryAttribute. #24744
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rolfbjarne
merged 1 commit into
main
from
dev/rolf/objcruntime-nullability-categoryattribute-06
Feb 19, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,39 +1,76 @@ | ||
| // | ||
| // CategoryAttribyute.cs: | ||
| // CategoryAttribute.cs: | ||
| // | ||
| // Authors: | ||
| // Rolf Bjarne Kvinge <rolf@xamarin.com> | ||
| // | ||
| // Copyright 2015 Xamarin Inc. | ||
|
|
||
| // Disable until we get around to enable + fix any issues. | ||
| #nullable disable | ||
| #nullable enable | ||
|
|
||
| namespace ObjCRuntime { | ||
| /// <include file="../../docs/api/ObjCRuntime/CategoryAttribute.xml" path="/Documentation/Docs[@DocId='T:ObjCRuntime.CategoryAttribute']/*" /> | ||
| /// <summary>Attribute used to flag a class as a category that extends another type.</summary> | ||
| /// <remarks> | ||
| /// <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> | ||
| /// <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> | ||
| /// <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> | ||
| /// <example> | ||
| /// <code lang="csharp"><![CDATA[ | ||
| /// // Make "shouldAutoRotate" return true for all UIViewControllers in the application | ||
| /// | ||
| /// [Category (typeof (UIViewController))] | ||
| /// static class MyViewControllerMethods { | ||
| /// [Export ("shouldAutorotate")] | ||
| /// static bool ShouldAutoRotate (this UIViewController self) | ||
| /// { | ||
| /// return true; | ||
| /// } | ||
| /// | ||
| /// [Export ("supportedInterfaceOrientations")] | ||
| /// static UIInterfaceOrientationMask SupportedRotations (this UIViewController self) | ||
| /// { | ||
| /// return UIInterfaceOrientationMask.All; | ||
| /// } | ||
| /// } | ||
| /// ]]></code> | ||
| /// </example> | ||
| /// <example> | ||
| /// <code lang="csharp"><![CDATA[ | ||
| /// // This example adds a native toUpper instance method to the NSString class, | ||
| /// // which can be invoked from Objective-C. | ||
| /// | ||
| /// [Category (typeof (NSString))] | ||
| /// public static class MyStringCategory | ||
| /// { | ||
| /// [Export ("toUpper")] | ||
| /// static string ToUpper (this NSString self) | ||
| /// { | ||
| /// return self.ToString ().ToUpper (); | ||
| /// } | ||
| /// } | ||
| /// ]]></code> | ||
| /// </example> | ||
| /// <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> | ||
| /// <para>See <see href="https://docs.microsoft.com/xamarin/cross-platform/deploy-test/linker">Custom Linker Configuration</see> for more information.</para> | ||
| /// </remarks> | ||
| [AttributeUsage (AttributeTargets.Class)] | ||
| public class CategoryAttribute : Attribute { | ||
| /// <param name="type">The Objective-C type to extend. This must be a subclass of NSObject (or NSObject itself).</param> | ||
| /// <summary>The type that this category extends.</summary> | ||
| /// <remarks> | ||
| /// </remarks> | ||
| /// <summary>Initializes a new instance of the <see cref="CategoryAttribute"/> class.</summary> | ||
| /// <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> | ||
| public CategoryAttribute (Type type) | ||
| { | ||
|
rolfbjarne marked this conversation as resolved.
|
||
| Type = type; | ||
| } | ||
|
|
||
| /// <summary>The type that this category extends.</summary> | ||
| /// <value> | ||
| /// </value> | ||
| /// <remarks> | ||
| /// </remarks> | ||
| /// <summary>Gets or sets the type that this category extends.</summary> | ||
| /// <value>The type this category extends.</value> | ||
| public Type Type { get; set; } | ||
| /// <summary>The name of the category.</summary> | ||
| /// <value> | ||
| /// </value> | ||
| /// <remarks> | ||
| /// <para>This must be a valid Objective-C type name, but is otherwise unused.</para> | ||
| /// </remarks> | ||
| public string Name { get; set; } | ||
|
|
||
| /// <summary>Gets or sets the name of the category.</summary> | ||
| /// <value>The category name, or <see langword="null"/> to use the managed type name.</value> | ||
| /// <remarks> | ||
| /// <para>This must be a valid Objective-C type name, but is otherwise unused.</para> | ||
| /// </remarks> | ||
| public string? Name { get; set; } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.