xamarin-macios/docs/bindas.md

3.2 KiB

How to add BindAs support for new types

Currently the BindAs code only supports converting between a certain set of types and NSValue, NSNumber and smart enums.

If your BindAs support does not involve NSValue or NSNumber, then the required changes are a bit more extensive than I explain here (but this is still a good starting point).

Sample code

The sample code is to support a new type for NSValue, the exact code locations will differ slightly for NSNumber (different switches, etc).

  1. Add a test (or three)

    • Add an entry to tests/test-libraries/testgenerator.cs for the new type. testgenerator.cs will generate the code required to test your new BindAs support for all known scenarios.

    • Any other manual tests should go in monotouch-test.

  2. Add native conversions functions to runtime/trampolines.m|h. In the sample code this is the two functions to convert between NSValue and NSDirectionalEdgeInsets:

    xamarin_nsdirectionaledgeinsets_to_nsvalue: trampolines.h#151, trampolines.m#889 xamarin_nsvalue_to_nsdirectionaledgeinsets: trampolines.h#116, trampolines.m#799

  3. Add a switch entry to trampolines.m#1007 to use the two new conversion functions.

  4. The registrar also needs to know (Registrar.cs#687).

  5. And the static registrar needs to know too, so that it can call the right native conversion function (StaticRegistrar.cs#3796, StaticRegistrar.cs#3830).

  6. Now there's just the generator support left (generator.cs#1223, generator.cs#1369).

  7. Finally run the following tests (at least)

  • All variations of monotouch-test (iOS/watchOS/tvOS) on both simulator and device.
  • link all on both simulator and device.