Generic and reflection-implemented functions

Source: Internet
Author: User

Use generic and reflection mechanisms to function the general, write it down, welcome to the Groove

The code example uses VB. Net

Imports System.Reflection

Module Module1

Sub Main ()

Dim Lst1 as List (of person) = New List (of the person) ()

Dim Lst2 as List (of person) = New List (of the person) ()

Dim Lstt as List (of person) = New List (of the 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

Generic and reflection-implemented functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.