3/30/2023 0 Comments Java reflection instantiate class![]() ![]() VBA Bean for use in a Visual Bean Application. Do you think that it has any real world applications? Hmmm. In truth, I will probably never use it but boy was it fun to get it working!!! Set Animal = ReflectionFactory.CreateObject(Choose(WorksheetFunction.RandBetween(1, 2), "Cat", "Dog")) Set puppy = ReflectionFactory.CreateObject("Dog") Set kitty = ReflectionFactory.CreateObject("Cat") Public Property Get CreateObject(ByVal ClassName As String) As ObjectĬase "IAnimal": Set CreateObject = New IAnimalĬase "MasterFactory": Set CreateObject = New MasterFactoryĭim kitty As IAnimal, puppy As IAnimal, Animal As IAnimal GetClassNameCase = vbTab & vbTab & "Case " & Chr(34) & ClassName & Chr(34) & ": Set CreateObject = New " & ClassName Private Function getClassNameCase(ByVal ClassName As String) As String Set getCodeModule = (TypeName(Me)).CodeModule Private Function getCodeModule() As Object If VBComp.Type = vbext_ct_ClassModule Then InsertLines StartLine + 1, getClassNameCase(CStr(ClassName))įor Each VBComp In Find("Case*New " & ClassName, 1, 1, 1, 1,, , True) Then Now, we use methods getClass and isAssignableFrom from Java Reflection to obtain an instance of representing the runtime classes of c and d. Public Sub AddClasses(ParamArray ClassNames() As Variant) Private Const vbext_ct_ClassModule As Long = 2 Private Const InsertAfter As String = "'" & ClassName InsertAfter" ![]() ReflectionFactory:Class Attribute VB_Name = "ReflectionFactory" Private Function IAnimal_Speak() As StringĬonst Phrase As String = "Meow Meow Meow" IAnimal:Interface Option ExplicitĬonst Phrase As String = "Ruff Ruff Ruff"ĭebug.Print TypeName(Me) " says:" """" Phrase """" This allows me to instantiate the Objects by their class names using the New keyword. The default instance of this class modifies itself by adding clauses to a Select Case. My quest for power over Reflection spawn the ReflectionFactory class. Eureka Reflection in VBA!! How evil! How inspiring!! So that began my journey into the darkness. In this SO post Dynamically choose class from string Alexander Platonov creates a Function on the fly and then uses Application.Run() to instantiate an Object by it's class name. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |