【问题标题】:How to tell if an object supports scalar comparisons?如何判断一个对象是否支持标量比较?
【发布时间】:2010-03-06 18:53:17
【问题描述】:

有没有人能快速了解一下我将如何检查给定类是否支持 >=运营商?

给定一个传入的对象,我正在寻找实现以下逻辑的代码:

If GetType(someObj).SupportsScalarComparisons() Then ... 

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

【问题讨论】:

    标签: vb.net comparison implementation


    【解决方案1】:

    我认为这是一个有趣的问题,所以我决定使用反射来提出解决方案。 (不知道除了反射还有没有其他办法。)


    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
    

    【讨论】:

    • 酷!这绝对完美!然后我想知道如何在我的泛型类型上实际调用比较运算符;使用 Func(Of Object, Object, Boolean) 类型的 Func 委托结束。感谢您的帮助和指导,干杯!
    【解决方案2】:

    尝试 Catch 任何你想做的事情......如果你 CATCH 那么你就做不到。

    【讨论】:

    • 好吧,我想在泛型中使用它,但我希望泛型只支持支持标量比较的类型,所以我将在泛型的构造函数中检查它,此时我不有任何有效的泛型参数实例来尝试比较。
    猜你喜欢
    • 1970-01-01
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-28
    • 1970-01-01
    • 2017-02-03
    相关资源
    最近更新 更多