Print Page | Close Window

GetEntities using a System.Type variable

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce Classic
Forum Discription: For .NET 2.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=949
Printed Date: 17-Sep-2025 at 3:27am


Topic: GetEntities using a System.Type variable
Posted By: f3rland
Subject: GetEntities using a System.Type variable
Date 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


-------------
F3



Replies:
Posted By: GregD
Date 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)


Posted By: f3rland
Date Posted: 30-Sep-2008 at 5:44am

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

Thanks!



-------------
F3



Print Page | Close Window