2010-03-06 105 views
6

沒有人有我怎麼會檢查,看看是否給定的類支持>=<運營商快速片段或方向?如何判斷一個對象是否支持標量比較?

給定一個對象傳遞,我在尋找實現以下邏輯代碼:

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

我不知道這是反思的情況下,還是?提前致謝。

回答

2

我認爲這是一個有趣的問題,所以我決定用反射來提出一個解決方案。如果再搭上你不能(我不知道是否有反射之外的另一種方式。)不管你想要做


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 
+0

酷!這工作絕對完美!我當時想知道如何去實際調用我的泛型類型的比較運算符;使用類型Func(Of Object,Object,Boolean)的Func委託結束。感謝您的幫助和指導,歡呼! – eidylon 2010-03-11 03:33:08

0

嘗試捕捉...。

+0

好吧,我想在泛型中使用它,但我希望泛型只支持支持標量比較的類型,所以我將在泛型的構造函數中檢查它,在這一點上,我沒有任何有效的泛型實例參數來嘗試比較。 – eidylon 2010-03-10 18:18:15

相關問題