为什么在VB6中C#null被翻译为空,而不是Nothing

tif*_*tif 15 c# vb6 com com-interop

我有一个引用VB6 DLL的C#应用​​程序.当我将C#中的null传递给VB6 dll函数时,null在VB6中被转换为值Empty(value),而不是Nothing(object).例如:

 // function in vb6 dll that referenced by c# app
 Public Sub TestFunc(ByVal oValue As Variant)
 {
   ...
   if oValue is Nothing then
     set oValue = someObject
   end if
   ...

 }

 // main c# code
 private void Form1_Load(object sender, EventArgs e)
 {
    object testObject = new object();
    testObject = null;
    TestFunc(testObject);
 }
Run Code Online (Sandbox Code Playgroud)

当我传递一个对象(非null)时,它将作为对象传递给VB6.但是当null传递给vb6时,它变为值类型为Empty,而不是对象类型Nothing.谁知道为什么?无论如何,当从c#app传递时,我可以强制null为VB6中的Nothing吗?

非常感谢.

Mar*_*rkJ 5

更多信息,而不是答案.我刚刚运行这个VB6临时程序来确认是否Nothing可以通过ByVal.有可能.

Private Sub Form_Load()
  Call TestSub(Nothing)
End Sub
Private Sub TestSub(ByVal vnt As Variant)
  Debug.Print VarType(Nothing)
  Debug.Print VarType(vnt)
  If vnt Is Nothing Then Debug.Print "vnt Is Nothing"
  If IsEmpty(vnt) Then Debug.Print "vnt Is Empty"
End Sub
Run Code Online (Sandbox Code Playgroud)

我得到了以下输出.请注意,9是vbObject,表示持有Object引用的Variant.

 9 
 9 
vnt Is Nothing
Run Code Online (Sandbox Code Playgroud)

我还没有测试过移动TestStub到另一个组件,但我认为它仍然有用.所以我认为.NET的COM编组可以做得更好.


小智 2

你有没有尝试过:

Public Sub TestFunc(ByVal oValue As Variant)
 {
   ...
   If oValue Is Nothing Then
     Set oValue = someObject
   ElseIf IsEmpty(oValue) Then
     Set oValue = someObject
   End If
   ...

 }
Run Code Online (Sandbox Code Playgroud)

编辑 - 我同意 Sander Rijken 关于为什么Empty被退回而不是被退回的答案null