简体   繁体   中英

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

In VB6, I can write something like this:

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

According to the modern VBA docs , which I realise aren't necessarily an exact match with their VB6 counterpart, this function behaves as follows:

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

The docs also remark:

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.

If I have an equivalent function in C#, which takes a parameter of type dynamic , what is the C# equivalent for IsObject?

The closest thing to an 'Object' in VBA is a reference type. You can use the following methods:

IsPrimitive

IsValueType

IsClass

These methods combined together would help you migrate that piece of code you have. Note however, that you need to have a strong understanding of types in C# - otherwise you will end up with code doing less or more than you wanted it to do.

Personally having decades of experience with VB6 and more than a decade in C# I would strongly reconsider my reasons for using these. Unlike in VBA, you extremely rarely need to stray away from a strongly typed syntax, ie the one where the at least basic properties of your variables are known in advance, such as whether it's a reference or value type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM