VB 函数 IsObject 的 C# 等价物是什么?

What is the C# equivalent for the VB function IsObject?

在VB6中,我可以这样写:

Private Sub MyMethod(ByVal someParameter Variant)
    If IsObject(someVariable) Then
        'do stuff
    Else
        'do something else
    End If
End Sub

根据 the modern VBA docs,我意识到这不一定与对应的 VB6 完全匹配,此函数的行为如下:

Returns a Boolean value indicating whether an identifier represents an object variable.

文档还指出:

IsObject is useful only in determining whether a Variant is of VarType vbObject. This could occur if the Variant actually references (or once referenced) an object, or if it contains Nothing.

如果我在 C# 中有一个等效函数,它采用 dynamic 类型的参数,IsObject 的 C# 等效函数是什么?

在 VBA 中最接近 'Object' 的是引用类型。您可以使用以下方法:

IsPrimitive

IsValueType

IsClass

这些方法结合在一起将帮助您迁移您拥有的那段代码。但是请注意,您需要对 types in C# 有深刻的理解 - 否则您最终会用代码做 lessmore 而不是你想要它做。

我个人拥有数十年的 VB6 经验和超过十年的 C# 经验,我会强烈地重新考虑我使用这些的原因。与 VBA 不同,您极少需要偏离强类型语法,即预先知道变量的至少基本属性的语法,例如它是引用类型还是值类型。