views:

68

answers:

2

Does anyone have a quick snippet or direction of how i would check to see if a given class supports the >, = and < operators?

Given an object passed in, I am looking for code that implements the following logic:

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

I don't know if this is a case for Reflection, or ? Thanks in advance.

+2  A: 

I thought this was an interesting question, so I decided to use reflection to come up with a solution. (I do not know if there is another way besides reflection.)


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
AMissico
Cool! That worked absolutely perfectly! I was then wondering how to go about actually calling the comparative operators on my generic type; wound up using Func delegates of type Func(Of Object, Object, Boolean). Thanks for your help and direction, cheers!
eidylon
A: 

Try Catch whatever you want to do... if you CATCH then you can't.

tobrien
Well, I want to use this in a generic, but I want the generic to only support types which support scalar comparisons, so i will check this in the generic's constructor, at which point i don't have any valid instances of the generic parameter to try comparing.
eidylon