[Mono-list] Writing daemons in C#

Paolo Molaro lupus@ximian.com
Wed, 20 Nov 2002 19:10:53 +0100


On 11/20/02 Jonathan Pryor wrote:
> I would suggest an alternative approach.  Use P/Invoke for everything,
> but instead of having the C# values differ from the platform-specific
> values, let them be the same.  Instead, use P/Invoke to also get the
> platform-specific values.
> 
> It could look like this:
> 
> 	public sealed class OpenMode {
> 		[DllImport ("posix-values")]
> 		private static extern int posix_get_O_CREAT ();
[...]
> The advantage to this is that you don't have to write as much custom
> code.  All of the `posix_get_CONSTANT()' functions can be generated with
> a single perl script:

It's better to make the script generate properties:

	public static int O_EXCL {
		get {return 0xwhatever;}
	}

The jit will inline the call and constant-fold if you use multiple
options (it cannot do that if you pinvoke a function).
Using properties still allows to have separate values on different
platforms without having a C library at runtime.

lupus

-- 
-----------------------------------------------------------------
lupus@debian.org                                     debian/rules
lupus@ximian.com                             Monkeys do it better