|
| 1 | +Additions allow you to add arbitrary C# to the generated classes |
| 2 | +before they are compiled. This can be helpful for providing convenience |
| 3 | +methods or adding pure C# classes. |
| 4 | + |
| 5 | +== Adding Methods to Generated Classes == |
| 6 | + |
| 7 | +Let's say the library being bound has a Rectangle class with a constructor |
| 8 | +that takes an x and y position, and a width and length size. It will look like |
| 9 | +this: |
| 10 | + |
| 11 | +public partial class Rectangle |
| 12 | +{ |
| 13 | + public Rectangle (int x, int y, int width, int height) |
| 14 | + { |
| 15 | + // JNI bindings |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +Imagine we want to add a constructor to this class that takes a Point and |
| 20 | +Size structure instead of 4 ints. We can add a new file called Rectangle.cs |
| 21 | +with a partial class containing our new method: |
| 22 | + |
| 23 | +public partial class Rectangle |
| 24 | +{ |
| 25 | + public Rectangle (Point location, Size size) : |
| 26 | + this (location.X, location.Y, size.Width, size.Height) |
| 27 | + { |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +At compile time, the additions class will be added to the generated class |
| 32 | +and the final assembly will a Rectangle class with both constructors. |
| 33 | + |
| 34 | + |
| 35 | +== Adding C# Classes == |
| 36 | + |
| 37 | +Another thing that can be done is adding fully C# managed classes to the |
| 38 | +generated library. In the above example, let's assume that there isn't a |
| 39 | +Point class available in Java or our library. The one we create doesn't need |
| 40 | +to interact with Java, so we'll create it like a normal class in C#. |
| 41 | + |
| 42 | +By adding a Point.cs file with this class, it will end up in the binding library: |
| 43 | + |
| 44 | +public class Point |
| 45 | +{ |
| 46 | + public int X { get; set; } |
| 47 | + public int Y { get; set; } |
| 48 | +} |
0 commit comments