From afa9ab58aeaedeb0ede835ef1bc1badbf0493273 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Wed, 15 Aug 2007 20:37:24 +0000 Subject: [PATCH] Add examples extending generated proxy class using partial classes and cscode typemap git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@9900 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Doc/Manual/CSharp.html | 139 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) diff --git a/Doc/Manual/CSharp.html b/Doc/Manual/CSharp.html index 6da7474ec..8852d96f1 100644 --- a/Doc/Manual/CSharp.html +++ b/Doc/Manual/CSharp.html @@ -29,6 +29,8 @@
  • Memory management when returning references to member variables
  • Memory management for objects passed to the C++ layer
  • Date marshalling using the csin typemap and associated attributes +
  • Turning wrapped classes into partial classes +
  • Extending proxy classes with additional C# code @@ -1720,6 +1722,143 @@ A few things to note:
  • The 'cshin' attribute is required for the SwigConstructAction constructor helper function so that the 2nd parameter is declared as ref dateOut instead of just dateOut. +

    17.5.4 Turning wrapped classes into partial classes

    + + +

    +C# supports the notion of partial classes whereby a class definition can be split into more than one file. +It is possible to turn the wrapped C++ class into a partial C# class using the csclassmodifiers typemap. +Consider a C++ class called ExtendMe: +

    + +
    +
    +class ExtendMe {
    +public:
    +  int Part1() { return 1; }
    +};
    +
    +
    + +

    +The default C# proxy class generated is: +

    + +
    +
    +public class ExtendMe : IDisposable {
    +  ...
    +  public int Part1() {
    +    ...
    +  }
    +}
    +
    +
    + +

    +The default csclassmodifiers typemap shipped with SWIG is +

    + +
    +
    +%typemap(csclassmodifiers) SWIGTYPE "public class"
    +
    +
    + +

    +If instead we use the following typemap to override this for just the ExtendMe class: +

    + +
    +
    +%typemap(csclassmodifiers) ExtendMe "public partial class"
    +
    +
    + +

    +The C# proxy class becomes a partial class: +

    + +
    +
    +public partial class ExtendMe : IDisposable {
    +  ...
    +  public int Part1() {
    +    ...
    +  }
    +}
    +
    +
    + +

    +You can then of course declare another part of the partial class elsewhere, for example: +

    + +
    +
    +public partial class ExtendMe : IDisposable {
    +  public int Part2() {
    +    return 2;
    +  }
    +}
    +
    +
    + +

    +and compile the following code: +

    + +
    +
    +ExtendMe em = new ExtendMe();
    +Console.WriteLine("part1: {0}", em.Part1());
    +Console.WriteLine("part2: {0}", em.Part2());
    +
    +
    + +

    +demonstrating that the class contains methods calling both unmanaged code - Part1() and managed code - Part2(). +The following example is an alternative approach to adding managed code to the generated proxy class. +

    + +

    17.5.5 Extending proxy classes with additional C# code

    + + +

    +The previous example showed how to use partial classes to add functionality to a generated C# proxy class. +It is also possible to extend a wrapped struct/class with C/C++ code by using the %extend directive. +A third approach is to add some C# methods into the generated proxy class with the cscode typemap. +If we declare the following typemap before SWIG parses the ExtendMe class used in the previous example +

    + +
    +
    +%typemap(cscode) ExtendMe %{
    +  public int Part3() {
    +    return 3;
    +  }
    +%}
    +
    +
    +
    + +

    +The generated C# proxy class will instead be: +

    + +
    +
    +public class ExtendMe : IDisposable {
    +  ...
    +  public int Part3() {
    +    return 3;
    +  }
    +  public int Part1() {
    +    ...
    +  }
    +}
    +
    +