編集

次の方法で共有


CategoryAttribute Class

Definition

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

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

Remarks

This attribute is applied to static user code classes and will surface all of the exported methods and properties (those that have 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 of your types surfaced to Objective-C.

All managed extension methods must be static, but it's 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 will add 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 only called from Objective-C), the managed linker will remove it. This can be avoided either by adding a PreserveAttribute attribute to the class, or by creating a custom linker definition file.

Constructors

CategoryAttribute(Type)

The type that this category extends.

Properties

Name

The name of the category.

Type

The type that this category extends.

Applies to

See also