Single C++/CLI method to wrap many type specific C functions

Posted by T33C on Stack Overflow See other posts from Stack Overflow or by T33C
Published on 2011-01-06T14:28:30Z Indexed on 2011/01/06 14:53 UTC
Read the original article Hit count: 160

Filed under:
|
|

I am wrapping a C library using C++/CLI so that the C library can be used easily from C# in a C#'ish way.

Some of the functions in this library are for putting a value into a container. There are no generics in C so there exists a function per type CLIB_SetBool(BOOL value), CLIB_SetInt(int value), CLIB_SetString(char* string) and so on.

To make it easier to use from C#, I have created a single Set function which takes a System::Object.

I have two related questions:

  1. With my method how would you use a switch statement on the type of System::Object to call the correct CLIB_Setxxxx function. [typeid is only for unmanaged code and I can't seem to use GetType.]

  2. Is there a better way to wrap these functions like using a Generic? [I started using template specialisation but then realised that C# doesn't see templates.]

Thanks.

© Stack Overflow or respective owner

Related posts about c#

Related posts about c