About Monkey 2 › Forums › Monkey 2 Programming Help › Reflecting multiple constructors
Tagged: reflection
This topic contains 2 replies, has 2 voices, and was last updated by
Ethernaut
1 year, 6 months ago.
Viewing 3 posts - 1 through 3 (of 3 total)
-
AuthorPosts
-
October 14, 2017 at 3:18 am #11095
Is there a way to make reflection work with classes that have multiple overloaded constructors?
I can make sure my own classes work fine, but I’m running into trouble with Mojo’s classes.Monkey1234567891011121314151617181920212223242526272829#Import "<std>"#Import "<mojo3d>"#Import "<reflection>"Using std..Using mojo3d..Function Main()' Local info := TypeInfo.GetType( "mojo3d.graphics.PbrMaterial" ) '<-- This has multiple constructors, doesn't workLocal info := TypeInfo.GetType( "mojo.input.MouseDevice" ) '<-- This has a single constructor with no arguments, works.'Lists declarationsFor Local d := Eachin info.GetDecls()Print d.NameNext'Try to invoke 'New()'Local v:VariantLocal constructor := info.GetDecl( "New" )v = constructor.Invoke( Null, Null )'Checks variant's typePrint "--------------------------------------------------"Print v.DynamicTypePrint "--------------------------------------------------"EndOctober 14, 2017 at 4:52 am #11097You need to use the version of GetDecl with the extra ‘type’ parameter – GetDecl with just the name param returns null if there are multiple decls with the same name.
Also, for ctors the function type you pass to GetDecl should have a Void return type. Something like this…
Monkey1234567891011121314151617181920212223242526272829303132333435363738#Import "<reflection>"Class CMethod New( x:Int )Print "x="+xEndMethod New( n:String,x:Int )Print "n="+n+", x="+xEndMethod Test:String( x:Int,y:Float )Return ""EndEndFunction Main()Local info:=TypeInfo.GetType( "default.C" )'Lists declarationsFor Local d := Eachin info.GetDecls()Print d.Name+":"+d.TypeNext'Try to invoke 'New()'Local v:VariantLocal ctor:=info.GetDecl( "New",Typeof<Void(String,Int)> )v = ctor.Invoke( Null,New Variant[]( "Yeah!",10 ) )'Checks variant's typePrint "--------------------------------------------------"Print v.DynamicTypePrint "--------------------------------------------------"EndOctober 14, 2017 at 7:27 am #11103Thanks!
-
AuthorPosts
Viewing 3 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic.