Send() and IVarPut()/IVarGet() in X#
Posted: Fri Aug 12, 2016 4:30 am
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:
This is the extension class that gives this functionality:
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:
Code: Select all
oObject:Send( cMethodName, oParameter )
oObject:PropertyPut( cPropertyName, oValue )
oValue := oObject:PropertyGet( cPropertyName )
Code: Select all
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