On Dec 22, 2010, at 6:35 PM, jeske wrote:
> Is there anything wrong with this pattern?

That depends on how you define "wrong." :-)

In theory, this is safe (safer, really, as you get more type checking).

In practice, it's more iffy for "social" reasons: unsafe code requires 'csc 
/unsafe', and some places don't like the '/unsafe' compiler flag.  The /unsafe' 
flag also marks the assembly as untrusted, so in certain CAS contexts you won't 
be able to use your assembly while you would be able to use the equivalent 
IntPtr code.

> At a deeper level, I wonder why Platform Invoke doesn't advocate using
> something like this more often, because it's alot safer than storing and
> passing everything around as IntPtr. 

Then there's the Common Language Subset (CLS) viewpoint: unsafe code is NOT 
part of the CLS, while IntPtr/etc. IS, so IntPtr is more "portable" across 
languages targeting .NET, and thus more "general." Advocating IntPtr allows 
Microsoft to keep their documentation shorter and just write about the more 
general approach.

 - Jon

_______________________________________________
Mono-list maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-list

Reply via email to