C#:如何传递null期待裁判的函数吗? [英] C#: How to pass null to a function expecting a ref?
问题描述
我有以下功能:
public static extern uint FILES_GetMemoryMapping(
[MarshalAs(UnmanagedType.LPStr)] string pPathFile,
out ushort Size,
[MarshalAs(UnmanagedType.LPStr)] string MapName,
out ushort PacketSize,
ref Mapping oMapping,
out byte PagesPerSector);
,我想这样的呼吁:
Which I would like to call like this:
FILES_GetMemoryMapping(MapFile, out size, MapName,
out PacketSize, null, out PagePerSector);
不幸的是,我无法通过空
在领域,需要键入 REF映射
并没有投我试过修复了这个。
Unfortunately, I cannot pass null
in a field that requires type ref Mapping
and no cast I've tried fixes this.
有什么建议?
推荐答案
我假设映射是一个结构?如果是这样你可以有 FILES_GetMemoryMapping()
原型不同签名的两个版本。对于您要传递的第二个重载空
,使参数的的IntPtr
,并使用 IntPtr.Zero
I'm assuming that Mapping is a structure? If so you can have two versions of the FILES_GetMemoryMapping()
prototype with different signatures. For the second overload where you want to pass null
, make the parameter an IntPtr
and use IntPtr.Zero
public static extern uint FILES_GetMemoryMapping(
[MarshalAs(UnmanagedType.LPStr)] string pPathFile,
out ushort Size,
[MarshalAs(UnmanagedType.LPStr)] string MapName,
out ushort PacketSize,
IntPtr oMapping,
out byte PagesPerSector);
通话例如:
Call example:
FILES_GetMemoryMapping(MapFile, out size, MapName,
out PacketSize, IntPtr.Zero, out PagePerSector);
如果映射实际上是一类,而不是一个结构,只需将它传递下来之前为空值
If Mapping is actually a class instead of a structure, just set the value to null before passing it down.
这篇关于C#:如何传递null期待裁判的函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!