January 22, 2019 at 1:17 am #15986
I’m finally to the point where I can create new class instances using reflection…
However, I’m stuck, as now I can’t do anything with the new variant. The variant.DyanmicType property does indeed report that I’ve created the class that I want. However, I can’t access anything within the class from this point, as I then get an error about the variant not having the property or method…
Simple sample:Monkey12345678910111213141516Class AMethod Test()Print "Success!"EndEndClass BField a:VariantEndFunction Main()Local instance:=New Binstance.a=New Ainstance.a.Test() ' Fails, because Value of type 'monkey.types.Variant' has no member named 'Test'EndJanuary 22, 2019 at 6:03 am #15988
You can store any Object in a Variant, and because of that you have to tell Monkey2 what it is.
With casting you can tell Monkey2 that instance.a is an instance of class A:Monkey123456789101112131415161718[crayon-5c6ba91fc4a1b557919372 inline="true" ]Class AMethod Test()Print "Success!"EndEndClass BField a:VariantEndFunction Main()Local instance:=New Binstance.a=New ACast<A>(instance.a).Test()End
Using that way you could use different classes that implement the same methods:Monkey123456789101112131415161718192021222324252627282930313233343536373839[crayon-5c6ba91fc4a22959554278 inline="true" ]Interface ITestMethod Test()EndClass A Implements ITestMethod Test()Print "Success A!"EndEndClass B Implements ITestMethod Test()Print "Success B!"EndEndClass C Implements ITestMethod Test()Print "Success C!"EndEndClass TheClassField a:VariantEndFunction Main()Local instance:=New TheClassinstance.a=New ACast<ITest>(instance.a).Test()instance.a=New BCast<ITest>(instance.a).Test()instance.a=New CCast<ITest>(instance.a).Test()EndJanuary 22, 2019 at 6:48 am #15990
Thanks for the reply.
I was afraid of that. It seems like there is no way to get a true object from the reflection system and have it usable as a normal object (without heaping up piles of GetType..GetDecl..Invoke..Cast.. and making the code overly complex)
I’m trying to build an XML parser that builds objects out of “skeleton” classes from the XML data. But I wanted to do so without special cases and Casts, as there are literally hundreds of skeleton classes. I just can’t find a way to build a true object from it’s name:String in monkey2. I’m guessing that I’m just going to have to bite the bullet and write a custom loader for each and every one of the hundreds of object types. Bummer
For example, I just need to make a function that:
instance:=ObjectFromName(“class_name”)January 22, 2019 at 7:18 am #15991
Could you use the following?
Creating an object just from a string, without telling Monkey2 what it is, is probably very hard if possible at all.January 22, 2019 at 10:17 am #15994
I’m going to take a hybrid approach – I’ll have to make routines to match the appropriate skeleton class to the XMLString – but then I’ll use reflection to fill in the fields in the class. At least I’m hoping this method will work
You must be logged in to reply to this topic.