Skip to content

Instantly share code, notes, and snippets.

@gavinb
Created November 4, 2020 08:17
Show Gist options
  • Save gavinb/7d1579524337739d4f6785aa49e7cc25 to your computer and use it in GitHub Desktop.
Save gavinb/7d1579524337739d4f6785aa49e7cc25 to your computer and use it in GitHub Desktop.
SWIG example of defining C# property
/*
swig -csharp -c++ -namespace MyControl -outdir ..\gen MyControl.i
*/
%module MyControl
%include <attribute.i>
%{
/* Includes the header in the wrapper code */
#include "..\include\MyControl.hpp"
%}
/* Parse the header file to generate wrappers */
%include "..\include\MyControl.hpp"
/* Define a SWIG attribute which will generate a C# Property */
/* Parameters: class, type, property_name, getter, setter */
%attribute(MyControl, int, Level, GetLevel, SetLevel);
@gavinb
Copy link
Author

gavinb commented Nov 4, 2020

Then in C# you can use this as:

    var control = new MyControl();
    System.Console.WriteLine($"{control.Level}");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment