- Module Module1
- Public Class User
- Public ID_User As Integer
- Public ID_Role As Integer
- Public Name As String
- Public Surname As String
- End Class
- Public Class Role
- Public ID_Role As Integer
- Public Name As String
- Public Description As String
- End Class
- Sub Main()
- Dim rs As New List(Of Role) From {New Role() With {.ID_Role = 1, .Name = "Default", .Description = ""}, _
- New Role() With {.ID_Role = 2, .Name = "J.Dev", .Description = ""}, _
- New Role() With {.ID_Role = 3, .Name = "S.Dev", .Description = ""}}
- Dim us As New List(Of User) From {New User() With {.ID_User = 1, .ID_Role = 1, .Name = "Fabio", .Surname = "Arosio"}, _
- New User() With {.ID_User = 1, .ID_Role = 2, .Name = "Claudio", .Surname = "Arosio"}, _
- New User() With {.ID_User = 1, .ID_Role = 1, .Name = "Lorenzo", .Surname = "Arosio"}}
- Dim us_rs = (From p In us Where p.ID_Role = 1 _
- Select New With {.User_Name = p.Name, _
- .User_Surname = p.Surname, _
- .Role = (From q In rs Where q.ID_Role = p.ID_Role Select q.Name).FirstOrDefault _
- }).ToList
- PrintAnonymousT(us_rs)
- Console.ReadLine()
- End Sub
- Public Sub PrintAnonymousT(AnonymousT As Object)
- 'Controllo se ho a che fare con una collection
- Dim t As Type = AnonymousT.GetType
- Dim IfGenericCollection = (From p In t.GetProperties Where p.MemberType = Reflection.MemberTypes.Property And p.Name = "Count" Select p).FirstOrDefault
- 'Se una collection la scorro e stampo le propriet dei membri
- If Not IsNothing(IfGenericCollection) Then
- For Each p In AnonymousT
- Dim t1 As Type = p.GetType
- Dim props = From q In t1.GetProperties Where q.MemberType = Reflection.MemberTypes.Property Select q
- For Each q In props
- Console.WriteLine("Propriet {0} = {1}", q.Name, q.GetValue(p, Nothing))
- Next
- Next
- End If
- End Sub
- End Module
Think about, case study, implementation... Pensieri, casi di studio e implementazioni, tutto quello che devo affrontare lavorando in c#.
martedì 20 marzo 2012
Tipi Anonimi e Reflection in VB
Iscriviti a:
Commenti sul post (Atom)
Nessun commento:
Posta un commento