Introspection TypeInfo from class name as string

If I only know the class name as String, is it possible to get Introspection TypeInfo from it ? Or is that a no go ?

Thanks

Correct. I believe the best you can do is some (potentially auto generated) lookup list like…

[code]Function GetTypeInfoByString(clsName As String) As Introspection.TypeInfo

Select Case clsName
Case “Listbox”
return GetTypeInfo(Listbox)

Case “MyClass”
return GetTypeInfo(MyClass)

Else
//?
End

End Function
[/code]

That is correct, no way to get TypeInfo from just a string. Wish it weren’t so, but it is.

If the object is instantiated somewhere, you could get it in a round about way via Runtime.IterateObjects, but it requires the class being currently instantiated somewhere and is not what most people are looking for in this case.

Maybe we should ask for an introspection method to get list of all objects?

Filled feedback case 35052 for a function to return all classes.

Hm I will work around it I guess, thanks.

As for 35052 then I think I would rather want something like Introspection.GetTypeInfo(“SomeClass”) which would return TypeInfo or nil if that application would not have the given class.

that’s also fine. But as I think they build all the classes when you access introspection first time. So having a getter to get all those type info classes, would be nice.

Are you saying that introspection data is build for all classes (property lists, method lists, etc…) when the very first call to GetType or Introspection.GetTypeInfo()? Do you know that for a fact or is there someone from Xojo that can confirm that?

Thanks for the hint Christian!

Also, I wonder if #35052 it is really possible. Say you have class Abc that is never used in your application directly. Xojo, when compiling, will strip that out (as I understand it) and it’ll never be part of the resulting binary. You then go to instantiate it via Introspection expecting it to be there…

if stripped it is gone.

Sorry, the type info is not created for all objects on first access.

It’s just that if you use Runtime.IterateObjects, you trigger gettypeInfo internally for all classes returned by the iterator.

[quote=124625:@Christian Schmitz]Sorry, the type info is not created for all objects on first access.

It’s just that if you use Runtime.IterateObjects, you trigger gettypeInfo internally for all classes returned by the iterator.[/quote]

OK, thanks. That makes more sense.

It’s not just classes that have this problem.
Just about everything in our project should be accessible through code - Interfaces, Windows, Pictures, etc…
I understand for optimization they can remove un-used components from compiled code, but really if you don’t use something like “Introspection.ProjectElements” then it can continue to do this.

My biggest annoyance is typically with Enums. Being able to convert an Enum into its string or int value or get an array of these values doesn’t seem like a hard thing to include that would save a ton of time writing helper methods to do this - let alone remembering to maintain and update these if you ever change the enum…

one solution for type info is to have code at startup of app to register all classes:

classes = new dictionary classes.append GetTypeInfo(MyClass1) classes.append GetTypeInfo(MyClass2) classes.append GetTypeInfo(MyClass3)

[quote=124691:@Christian Schmitz]one solution for type info is to have code at startup of app to register all classes:

classes = new dictionary classes.append GetTypeInfo(MyClass1) classes.append GetTypeInfo(MyClass2) classes.append GetTypeInfo(MyClass3)[/quote]

That is a technique I use in a few places but with a little difference:

Module InstrospectionHelper
  Private Property registry As Dictionary

  Public Sub RegisterClass(o As Introspection.TypeInfo)
    If registry Is Nil Then
      registry = New Dictionary
    End If

    registry.Value(o.FullName) = o
  End Sub

  Public Function GetClass(fullName As String) As Introspection.TypeInfo
    If registry Is Nil Then
      Return Nil
    End If

    Return registry.Lookup(name, Nil)
  End Function
End Module

There are quite a few other methods I use when dealing with introspection, but that’s the basics. Obviously you can only then access by name classes that you have registered. Also, by registering them you loose the ability for Xojo to optimize them out of your binary if not needed. So it is all a trade off.

BTW… I am doing a Webinar on Introspection at the end of Sept if interested (speaking in General, not really to you Christian, as I know you could easily teach the webinar), it will cover quite a bit of this and other things as well (https://xojo.com/support/webinar.php).