diff --git a/Doc/Manual/CSharp.html b/Doc/Manual/CSharp.html index a7be3ed24..fd04ee7b6 100644 --- a/Doc/Manual/CSharp.html +++ b/Doc/Manual/CSharp.html @@ -38,6 +38,12 @@ Swig C# works equally well on non-Microsoft operating systems such as Linux, Sol Mono and Portable.NET.

+

+To get the most out of this chapter an understanding of interop is required. +The Microsoft Developer Network (MSDN) has a good reference guide in a section titled "Interop Marshaling". +Monodoc, available from the Mono project, has a very useful section titled Interop with native libraries. +

+

16.2 Differences to the Java module

@@ -128,6 +134,7 @@ javadestruct_derived -> csdestruct_derived
 csvarin                     C# code property set typemap
 csvarout                    C# code property get typemap
+csattributes                C# attributes for attaching to proxy classes/enums
 
@@ -171,7 +178,7 @@ The intermediary classname has PINVOKE appended after the module name i
  • Support for asymmetric type marshalling. The 'ctype', 'imtype' and 'cstype' typemaps support an optional out attribute which is used for output types. -If this typemap attribute is specified, then the type specified in the attribute is used for output types. +If this typemap attribute is specified, then the type specified in the attribute is used for output types and the type specified in the typemap itself is used for the input type. If this typemap attribute is not specified, then the type used for both input and output is the type specified in the typemap. An example shows that char * could be marshalled in different ways, @@ -196,6 +203,105 @@ public static extern IntPtr function(string jarg1);

  • +
  • +

    +Support for type attributes. +The 'imtype' and 'cstype' typemaps can have an optional inattributes and outattributes typemap attribute. +There are C# attributes and typemap attributes, don't get confused!! +The C# attributes specified in these typemap attributes are generated wherever the type is used in the C# wrappers. +These can be used to specify any C# attribute associated with a C/C++ type, but are more typically used for the C# MarshalAs attribute. +For example: +

    + +
    +
    +%typemap(imtype,
    +         inattributes="[MarshalAs(UnmanagedType.LPStr)]",
    +         outattributes="[return: MarshalAs(UnmanagedType.LPStr)]") const char * "String"
    +
    +const char * GetMsg() {}
    +void SetMsg(const char *msg) {}
    +
    +
    + +

    +The intermediary class will then have the marshalling as specified by everything in the 'imtype' typemap: +

    + +
    +
    +class examplePINVOKE {
    +  ...
    +  [DllImport("example", EntryPoint="CSharp_GetMsg")]
    +  [return: MarshalAs(UnmanagedType.LPStr)]
    +  public static extern String GetMsg();
    +
    +  [DllImport("example", EntryPoint="CSharp_SetMsg")]
    +  public static extern void SetMsg([MarshalAs(UnmanagedType.LPStr)]String jarg1);
    +}
    +
    +
    + +

    +Note that the DllImport attribute is always generated, irrespective of any additional attributes specified. +

    + +

    +These attributes are associated with the C/C++ parameter type or return type, which is subtely different to +the attribute features and typemaps covered next. +Note that all these different C# attributes can be combined so that a method has more than one attribute. +

    +
  • + +
  • +

    +Support for attaching C# attributes to wrapped methods and variables. +This is done using the %csattributes feature, see %feature directives. +Note that C# attributes are attached to proxy classes and enums using the csattributes typemap. +For example, imagine we have a custom attribute class, ThreadSafeAttribute, for labelling thread safety. +The following SWIG code shows how to attach this C# attribute to some methods and the class declaration itself: +

    + +
    +
    +%typemap(csattributes) AClass          "[ThreadSafe]"
    +%csattributes AClass::AClass(double d) "[ThreadSafe(false)]"
    +%csattributes AClass::AMethod()        "[ThreadSafe(true)]"
    +
    +%inline %{
    +class AClass {
    +public:
    +  AClass(double a) {}
    +  void AMethod() {}
    +};
    +%}
    +
    +
    + +

    +will generate a C# proxy class: +

    + +
    +
    +[ThreadSafe]
    +public class AClass : IDisposable {
    +  ...
    +  [ThreadSafe(false)]
    +  public AClass(double a) ...
    +
    +  [ThreadSafe(true)]
    +  public void AMethod() ...
    +}
    +
    +
    + +

    +If C# attributes need adding to the set or get part of C# properties, when wrapping C/C++ variables, +they can be added using the 'csvarin' and 'csvarout' typemaps respectively. +

    +
  • +