CategoryAttribute Class

Definition

Attribute used to flag a class as a category that extends another type.

[System.AttributeUsage(System.AttributeTargets.Class)]
public class CategoryAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class)>]
type CategoryAttribute = class
    inherit Attribute
Inheritance
CategoryAttribute
Attributes

Remarks

This attribute is applied to static user-code classes and surfaces all exported methods and properties (those with the ExportAttribute) to the provided system type.

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.

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#:

// 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;
    }
}
// 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 ();
    }
}

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 PreserveAttribute attribute to the class, or by creating a custom linker definition file.

See Custom Linker Configuration for more information.

Constructors

Name Description
CategoryAttribute(Type)

Initializes a new instance of the CategoryAttribute class.

Properties

Name Description
Name

Gets or sets the name of the category.

Type

Gets or sets the type that this category extends.

Applies to

See also