用泛型和反射实现函数通用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用泛型和反射实现函数通用相关的知识,希望对你有一定的参考价值。

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

  代码示例使用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

以上是关于用泛型和反射实现函数通用的主要内容,如果未能解决你的问题,请参考以下文章

总结java的泛型和内部类

Java 泛型和可变参数

利用泛型和反射实现IDataReader转实体

为什么要使用泛型和迭代器

编写高质量代码:改善Java程序的151个建议(第7章:泛型和反射___建议106~109)

播放器项目 用泛型集合实现 歌词显示功能