Object extensions

This forum is meant for examples of X# code.

Post Reply
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Object extensions

Post by wriedmann »

Hello,

I'm used from VO to write code that works dynamically. Therefore I have written several extension methods for the object class. Maybe this code helps someone:

Code: Select all

using System                        
using System.Text
using System.Runtime.InteropServices
using System.Reflection 
using System.Diagnostics
using System.Collections.Generic

begin namespace rdm.BaseLib

class ObjectExtensions

	constructor()  
		super()
	return

static method IsMethod( self oObject as object, cMethod as string ) as logic
	local oType as System.Type
	local lReturn as logic
	
	if oObject == null_object
		lReturn		:= false
	else
		oType 		:= oObject:GetType()
		if oType:GetMethod( cMethod ) == null_object
			lReturn		:= false
		else
			lReturn		:= true
		endif
	endif
	
	return lReturn

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
	
	oType				:= null_object
	oInfo				:= null_object
	
	return oReturn              
	
static method ClassName( self oObject as object ) as string 
	local cReturn		as string
	
	cReturn				:= oObject:GetType():Name
	
	return cReturn	    
	
static method PropertyNames( self oObject as object ) as string[]
	local aReturn		as string[]
	local nLen			as int
	local nI			as int       
	local oProperties	as System.Reflection.PropertyInfo[]
                          
	oProperties		:= oObject:GetType():GetProperties()	
	nLen			:= oProperties:Length
	aReturn			:= string[]{nLen}
	for nI := 1 upto nLen
		aReturn[nI]		:= oProperties[nI]:Name
	next
	oProperties		:= null_object
		
	return aReturn	
	
static method IsProperty( self oObject as object, cPropertyName as string ) as logic
	local aProperties	as string[]
	local lReturn		as logic
	local nLen			as int
	local nI			as int       
	
	aProperties		:= PropertyNames( oObject )
	cPropertyName	:= cPropertyName:ToLower()
	nLen			:= aProperties:Length
	lReturn			:= false
	for nI := 1 upto nLen
		if aProperties[nI]:ToLower() == cPropertyName
			lReturn			:= true
			exit
		endif
	next
	
	return lReturn
	
static method FixPropertyName( self oObject as object, cPropertyName as string ) as string
	local aProperties	as string[]
	local cReturn		as string
	local nLen			as int
	local nI			as int       
	
	aProperties		:= PropertyNames( oObject )
	cPropertyName	:= cPropertyName:ToLower()
	nLen			:= aProperties:Length
	cReturn			:= cPropertyName
	for nI := 1 upto nLen
		if aProperties[nI]:ToLower() == cPropertyName
			cReturn			:= aProperties[nI]
			exit
		endif
	next
	
	return cReturn

static method GetPropertyValue( self oObject as object, cPropertyName as string ) as object
// Attention: the property name must respect the correct case!!!!
	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 SetPropertyValue( self oObject as object, cPropertyName as string, oValue as object ) as void
// Attention: the property name must respect the correct case!!!!
	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

end namespace
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply