zoukankan      html  css  js  c++  java
  • 用泛型和反射实现函数通用

      使用泛型和反射机制事项函数的通用,写下来,欢迎吐槽

      代码示例使用vb. net

    Imports System.Reflection

    Module Module1

        Sub Main()

            Dim lst1 As List(Of Person) = New List(Of Person)()

            Dim lst2 As List(Of Person) = New List(Of Person)()

            Dim lstT As List(Of Person) = New List(Of Person)()

            For i As Integer = 1 To 10

                Dim p As Person = New Person() With {.name = "A" + i.ToString, .age = i}

                lst1.Add(p)

            Next

            For i As Integer = 5 To 20

                Dim p As Person = New Person() With {.name = "A" + i.ToString, .age = i}

                lst2.Add(p)

            Next

            lstT = New Common().MyFilter(Of Person)(lst1, lst2)

            For Each itm As Person In lstT

                Console.WriteLine(itm.name)

            Next

            Console.WriteLine("========================")

            Dim lst3 As List(Of Animal) = New List(Of Animal)()

            Dim lst4 As List(Of Animal) = New List(Of Animal)()

            Dim lstM As List(Of Animal) = New List(Of Animal)()

            For i As Integer = 1 To 10

                Dim p As Animal = New Animal() With {.name = "B" + i.ToString, .ID = i}

                lst3.Add(p)

            Next

            For i As Integer = 5 To 20

                Dim p As Animal = New Animal() With {.name = "B" + i.ToString, .ID = i}

                lst4.Add(p)

            Next

            lstM = New Common().MyFilter(Of Animal)(lst3, lst4)

            For Each itm As Animal In lstM

                Console.WriteLine(itm.name)

            Next

            Console.WriteLine("Over OK!")

            Console.Read()

        End Sub

     

     

    End Module

    Class Person

        Public name As String

        Public age As Integer

    End Class

     

    Class Animal

        Public name As String

        Public ID As String

    End Class

     

    Class Common

        Public Function MyFilter(Of T)(ByVal lst1 As List(Of T), ByVal lst2 As List(Of T)) As List(Of T)

            Dim lstT As List(Of T) = New List(Of T)()

            lstT = lst1.Where(Function(x As T)

                                  If lst2.Any(Function(u As T)

                                                  Return MyCompare(x, u)

                                              End Function) Then

                                      Return False

                                  Else

                                      Return True

                                  End If

                              End Function).ToList()

            Return lstT

        End Function

     

        Public Shared Function MyCompare(Of T)(ByVal t1 As T, ByVal t2 As T) As Boolean

            Dim tmp1 As Type = t1.GetType()

            Dim fields1 As FieldInfo() = tmp1.GetFields()

     

            Dim tmp2 As Type = t2.GetType()

            Dim fields2 As FieldInfo() = tmp2.GetFields()

     

            For i As Integer = 0 To fields1.Count - 1

                If Not (fields1(i).GetValue(t1)).Equals((fields2(i).GetValue(t2))) Then

                    Return False

                End If

            Next

            Return True

        End Function

    End Class

  • 相关阅读:
    递归二分法和另类二分法(不推荐,因为占用资源)
    内置函数汇总
    非递归二分法
    2018.10.23习题随笔
    java中的类修饰符、成员变量修饰符、方法修饰符。
    js函数中的this关键字
    HTML5 <script>元素async,defer异步加载
    那些迷糊人的回调
    js函数prototype属性学习(二)
    js函数prototype属性学习(一)
  • 原文地址:https://www.cnblogs.com/aaron-song/p/5503148.html
Copyright © 2011-2022 走看看