Browse Source

Updated reflection.

Mark Sibly 7 năm trước cách đây
mục cha
commit
09cc6409af

+ 132 - 0
modules/reflection/reflection.monkey2

@@ -1,2 +1,134 @@
 
 Namespace reflection
+
+#Import "<std>"
+
+Using std..
+
+#rem monkeydoc Debug reflected types.
+
+Prints all reflected types and decls.
+
+#end
+Function DebugTypes()
+	
+	For Local type:=Eachin TypeInfo.GetTypes()
+		
+		Print type
+		
+		For Local decl:=Eachin type.GetDecls()
+			
+			Print "  "+decl
+		Next
+		
+		Print ""
+		
+	Next
+	
+End
+
+#rem monkeydoc Gets type extensions for a given type.
+
+Returns an array containing all type extensions for a given type.
+
+#end
+Function GetTypeExtensions:TypeInfo[]( type:TypeInfo )
+
+	Global _typeExts:StringMap<TypeInfo[]>
+	
+	If Not _typeExts
+	
+		Local typeExts:=New StringMap<Stack<TypeInfo>>
+		
+		For Local type:=Eachin TypeInfo.GetTypes()
+			
+			If Not type.Kind.EndsWith( " Extension" ) Continue
+			
+			If Not typeExts.Contains( type.SuperType ) typeExts[type.SuperType]=New Stack<TypeInfo>
+	
+			typeExts[type.SuperType].Add( type )
+		
+		Next
+		
+		_typeExts=New StringMap<TypeInfo[]>
+		
+		For Local it:=Eachin typeExts
+			
+			_typeExts[it.Key]=it.Value.ToArray()
+		
+		Next
+	
+	Endif
+	
+	Return _typeExts[type]
+End
+
+#rem monkeydoc Gets the value of a property.
+#end
+Function GetProperty:Variant( name:String,instance:Variant )
+	
+	Local type:=instance.Type
+	
+	While type
+		
+		Local decl:=type.GetDecl( name )
+		
+		If decl And decl.Kind="Property" And decl.Name=name Return decl.Get( instance )
+		
+		For Local type:=Eachin GetTypeExtensions( type )
+			
+			Local decl:=type.GetDecl( name )
+
+			If decl And decl.Kind="Property" And decl.Name=name Return decl.Get( instance )
+		
+		Next
+		
+		type=type.SuperType
+	
+	Wend
+	
+	Return Null
+End
+
+Function GetProperty<T>:T( name:String,instance:Variant )
+	
+	Local value:=GetProperty( name,instance )
+	
+	If value Return Cast<T>( value )
+	
+	Return Null
+End
+
+#rem monkeydoc Sets a property to a value.
+#end
+Function SetProperty:Bool( name:String,instance:Variant,value:Variant )
+	
+	Local type:=instance.Type
+	
+	While type
+		
+		Local decl:=type.GetDecl( name )
+		
+		If decl And decl.Kind="Property" And decl.Name=name
+			decl.Set( instance,value )
+			Return True
+		Endif
+		
+		For Local type:=Eachin GetTypeExtensions( type )
+
+			Local decl:=type.GetDecl( name )
+			
+			If decl And decl.Kind="Property" And decl.Name=name
+				decl.Set( instance,value )
+				Return True
+			Endif
+		
+		Next
+		
+		type=type.SuperType
+	
+	Wend
+	
+	Return False
+
+End

+ 61 - 0
modules/reflection/tests/property.monkey2

@@ -0,0 +1,61 @@
+
+Namespace test
+
+#Import "<reflection>"
+#Import "<std>"
+#Import "<mojo>"
+#Import "<mojox>"
+#Import "<mojo3d>"
+
+#Reflect test
+#Reflect std
+#Reflect mojo
+#Reflect mojo3d
+#Reflect mojox
+
+Using reflection..
+Using std..
+
+Class E
+	
+	Field _name:="Brian"
+	
+	Field _pos:=New Vec3f( 1,2,3 )
+	
+	Property Name:String()
+		
+		Return _name
+	
+	Setter( name:String )
+		
+		_name=name
+	End
+	
+End
+
+Class E Extension
+	
+	Property Position:Vec3f()
+		
+		Return _pos
+	
+	Setter( pos:Vec3f )
+		
+		_pos=pos
+	End
+	
+End
+
+Function Main()
+
+	Local e:=New E
+	
+	SetProperty( "Name",e,"Douglas" )
+	
+	Print GetProperty<String>( "Name",e )
+	
+	SetProperty( "Position",e,New Vec3f( 4,5,6 ) )
+	
+	Print GetProperty<Vec3f>( "Position",e )
+	
+End