PropertyReflector

Helper class that allows you to examine an object’s properties and to dynamically get and set their values.

Description

The PropertyReflector class allows you to examine an object’s properties and to dynamically get and set their values.

Important points

  • This class has symmetric implementation across all languages supported by the Pip.Services toolkit and it is used to support dynamic data processing.
  • Because all languages have different casing and case sensitivity rules, the PropertyReflector class treats all property names as case insensitive.

Static methods

getProperties

Get values of all properties in specified object and returns them as a map.

public static getProperties(obj: any): any

  • obj: any - an object to get properties from.
  • returns: any - a map, containing the names of the object’s properties and their values.

getProperty

Gets value of object property specified by its name.

public static getProperty(obj: any, name: string): any

  • obj: any - an object to read property from.
  • name: string - a name of the property to get.
  • returns: any - the property value or null if property doesn’t exist or introspection failed.

getPropertyNames

Gets names of all properties implemented in specified object.

public static getPropertyNames(obj: any): string[]

  • obj: any - an objec to introspect.
  • returns: string[] - a list with property names.

hasProperty

Checks if object has a property with specified name.

public static hasProperty(obj: any, name: string): boolean

  • obj: any - an object to introspect.
  • name: string - a name of the property to check.
  • returns: boolean - true if the object has the property and false if it doesn’t.

setProperties

Sets values of some (all) object properties.

If some properties do not exist or introspection fails they are just silently skipped and no errors thrown.

public static setProperties(obj: any, values: any): void

  • obj: any - an object to write properties to.
  • values: any - a map, containing property names and their values.

setProperty

Sets value of object property specified by its name.

If the property does not exist or introspection fails this method doesn’t do anything and doesn’t any throw errors.

public static setProperty(obj: any, name: string, value: any): void

  • obj: any - an object to write property to.
  • name: string - a name of the property to set.
  • value: any - a new value for the property to set.

Examples

let myObj = new MyObject();
   
let properties = PropertyReflector.getPropertyNames();
PropertyReflector.hasProperty(myObj, "myProperty");

let value = PropertyReflector.getProperty(myObj, "myProperty");
PropertyReflector.setProperty(myObj, "myProperty", 123);