Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

Send() and IVarPut()/IVarGet() in X# 12 Aug 2016 06:30 #303

  • wriedmann
  • wriedmann's Avatar
  • Topic Author


  • Posts: 3245
  • Sometimes, you could need a functionality like the Send(), IVarPut() and IVarGet() functions from Clipper also in X#
    Fortunately, thanks to Reflection, this is possible, and thanks to the possibility to use extension methods, also easier to use.

    A sample use could be:
    oObject:Send( cMethodName, oParameter ) 
    oObject:PropertyPut( cPropertyName, oValue )
    oValue := oObject:PropertyGet( cPropertyName )

    This is the extension class that gives this functionality:
    using System                        
    using System.Text
    using System.Runtime.InteropServices
    using System.Reflection 
    using System.Diagnostics
    
    static class ObjectExtensions
    
    static method Send( self oObject as object, cMethod as string, aParameters as object[] ) as object 
    local oType as System.Type
    local oReturn as object
    local oInfo as MethodInfo
    	
    oType := oObject:GetType()
    oReturn := null_object
    oInfo := oType:GetMethod( cMethod )
    if oInfo != null_object
        oReturn := oInfo:Invoke( oObject, aParameters )
    endif
    	
    return oReturn              
    
    static method PropertyGet( self oObject as object, cPropertyName as string ) as object
    local oReturn as object	
    local oInfo as System.Reflection.PropertyInfo
    	
    oInfo := oObject:GetType():GetProperty( cPropertyName ) 
    if oInfo != null_object                                  
        oReturn := oInfo:GetValue( oObject, null_object ) 
    else
        oReturn			:= null_object
    endif
    	
    return oReturn
    
    static method PropertyPut( self oObject as object, cPropertyName as string, oValue as object ) as void
    local oInfo as System.Reflection.PropertyInfo
    	
    oInfo := oObject:GetType():GetProperty( cPropertyName )
    if oInfo != null_object
        oInfo:SetValue( oObject, oValue, null_object )                        
    endif
    	
    return
    
    end class
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    Send() and IVarPut()/IVarGet() in X# 15 Aug 2016 16:27 #304

    • robert
    • robert's Avatar


  • Posts: 3286
  • Wolfgang,

    Nice how you have used the extension method to extend the OBJECT type and therefore make the methods available in every type.
    This is in fact very similar to what happens in the Vulcan Runtime and what will happen in the new XSharp runtime for the Send() function and IVarGet() and IVarPut() functions.

    The biggest difference for these functions is that they cache the MethodInfo and PropertyInfo and for properties that they also do a lookup for (public) fields, since the VO language allows late bound access to public fields as well.

    Robert
    XSharp Development Team
    The Netherlands

    Please Log in or Create an account to join the conversation.

    • Page:
    • 1