vb.net – 如何判断对象是否支持标量比较?

有没有人有一个快速的片段或指示如何检查给定的类是否支持>,=和<操作符? 给定一个传入的对象,我正在寻找实现以下逻辑的代码
If GetType(someObj).SupportsScalarComparisons() Then ...

我不知道这是反映的情况,还是?提前致谢.

我认为这是一个有趣的问题,所以我决定使用反射来提出解决方案. (我不知道除了反思之外还有另一种方式.)
Imports System.Reflection

Module MainModule

    Sub Main()

        'primitive,value type
        If GetType(Integer).SupportsScalarComparisons Then
            Debug.WriteLine("Integer supports comparisions")
        Else
            Debug.WriteLine("Integer does not support comparisions")
        End If

        'non-primitive,value type
        If GetType(Decimal).SupportsScalarComparisons Then
            Debug.WriteLine("Decimal supports comparisions")
        Else
            Debug.WriteLine("Decimal does not support comparisions")
        End If

        'non-primitive,object type
        If GetType(Version).SupportsScalarComparisons Then
            Debug.WriteLine("Version supports comparisions")
        Else
            Debug.WriteLine("Version does not support comparisions")
        End If

        'non-primitive,object type
        If GetType(String).SupportsScalarComparisons Then
            Debug.WriteLine("String supports comparisions")
        Else
            Debug.WriteLine("String does not support comparisions")
        End If

        'Integer supports comparisions
        'Decimal supports comparisions
        'Version supports comparisions
        'String does not support comparisions

    End Sub

    Public Sub Dump(ByVal type As Type)
        Dim oMethod() As MethodInfo = type.getmethods(BindingFlags.Static Or BindingFlags.Public)
        For Each o As MethodInfo In oMethod
            Debug.WriteLine(o.Name)
        Next
    End Sub

End Module

Public Module TypeExtensions

    <System.Runtime.CompilerServices.Extension()> _
    Public Function SupportsScalarComparisons(ByVal obj As Type) As Boolean
        Static Methods() As String = {"op_GreaterThan","op_Equality","op_Lessthan"}

        If obj.IsPrimitive Then
            Return True
        End If

        For Each sMethodName As String In Methods
            Dim oMethod As MethodInfo = obj.getmethod(sMethodName,BindingFlags.Public Or BindingFlags.Static)
            If oMethod Is nothing Then
                'does not support
                Return False
            End If
        Next

        Return True

        'List is from MSDN Library index
        'op_Addition
        'op_BitwiseAnd
        'op_BitwiSEOr
        'op_Decrement
        'op_Division
        'op_Equality
        'op_ExculsiveOr
        'op_Explicit
        'op_False
        'op_GreaterThan
        'op_GreaterThanorEqual
        'op_Implicit
        'op_Increment
        'op_Inequality
        'op_LogicalNot
        'op_Lessthan
        'op_LessthanorEqual
        'op_Modulus
        'op_Multiply
        'op_OnesComplement
        'op_Subtraction
        'op_True
        'op_UnaryNegation
        'op_UnaryPlus

    End Function

End Module

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...