New Posts New Posts RSS Feed: GetEntities using a System.Type variable
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

GetEntities using a System.Type variable

 Post Reply Post Reply
Author
f3rland View Drop Down
Newbie
Newbie
Avatar

Joined: 13-Sep-2007
Location: Canada
Posts: 25
Post Options Post Options   Quote f3rland Quote  Post ReplyReply Direct Link To This Post Topic: GetEntities using a System.Type variable
    Posted: 25-Sep-2008 at 6:20am

Is there a way to retrieve Entities using a System.Type variable instead of a generics (Of T) that cannot be set in a variable?

I would like to be able to do something like that :

   Private Sub TestReflex()
      Dim oAms As Reflection.Assembly = Reflection.Assembly.GetCallingAssembly
      For Each oType As Type In oAms.GetTypes
         If oType.BaseType Is GetType(Entity) Then
            PrintAll(oType)
         End If
      Next
   End Sub
   Private Sub PrintAll(ByVal pType As System.Type)
      For Each oEnt As Entity In DefaultManager.GetEntities(Of pType)() '<-- Does not compile
         PrintValues(Of pType.BaseType) (CType(oEnt, pType.BaseType)) '<-- Does not compile
      Next
   End Sub
   Private Sub PrintValues(Of T)(ByVal pItem As T)
      Dim oProperties() As Reflection.PropertyInfo
       oProperties = GetType(T).GetProperties(Reflection.BindingFlags.DeclaredOnly _
                                             Or Reflection.BindingFlags.Instance _
                                             Or Reflection.BindingFlags.Public)
      For Each oProp As Reflection.PropertyInfo In oProperties
         If oProp.CanRead AndAlso oProp.CanWrite Then
            Console.WriteLine("{0} = {1}", oProp.Name, oProp.GetValue(pItem, Nothing))
         ElseIf oProp.CanRead Then
            Console.WriteLine("{0}* = {1}", oProp.Name, oProp.GetValue(pItem, Nothing))
         End If
      Next
   End Sub
 
Thanks for any idea


Edited by f3rland - 25-Sep-2008 at 12:10pm
F3
Back to Top
GregD View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 374
Post Options Post Options   Quote GregD Quote  Post ReplyReply Direct Link To This Post Posted: 29-Sep-2008 at 3:02pm
Try the non-generic form of GetEntities, overload that accepts a type:
 
     For Each oEnt As Entity In DefaultManager.GetEntities(pType)
Back to Top
f3rland View Drop Down
Newbie
Newbie
Avatar

Joined: 13-Sep-2007
Location: Canada
Posts: 25
Post Options Post Options   Quote f3rland Quote  Post ReplyReply Direct Link To This Post Posted: 30-Sep-2008 at 5:44am

I never noticed that overload.. I'm used to use the dynamic entity type...

Thanks!

F3
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down