Pedro Lopes · Feb 3

List properties

I would like to know how I can make a utility to retrieve all the names of properties of a class. Any idea?

0 139
Discussion (5)3
Log in or sign up to continue

Hi Pedro,

You can retrieve this information querying the class %Dictionary.PropertyDefinition.

An example:

FROM %Dictionary.PropertyDefinition
WHERE parent = "mypackage.MyClassName"

The field parent is fully qualified name of the class that you want to list properties name. 

Simple and perfect, I just added "ORDER BY SequenceNumber" to suit my needs. Thanks

Hi Pedro,

In addition to the reply of @Cristiano Silva ,  you can use %Dictionary.CompiledClass instead of PropertyDefinition to retrieve also properties in parents classes.

There is a query available : 

select *
from %Dictionary.CompiledClassQuery_MemberSummary('','a')

See class reference

query MemberSummary(classname As %String, kind As %String)
Selects Name As %String(MAXLEN=256)

Return a list of members of this specific kind which is one of the following:

  • a - Property
  • f - Foreign key
  • i - Index
  • j - Projection
  • m - Method
  • n - Constraint
  • o - System method
  • p - Parameter
  • q - Query
  • s - Storage defintion
  • t - Trigger
  • u - Comment text block
  • x - XData

Create an abstract class and add it to all your classes, where a list of properties (for whatever reason) is needed 

Class DC.ClassInfo [ Abstract ]

/// Return property info: %PropNames(all)
/// all: 1 = Return a list of all properties<br>
///      0 = Return a list of storable properties only
ClassMethod PropNames(all = 0) As %String [ CodeMode = objectgenerator ]
    s (prop(0),prop(1))=""
    f i=1:1:%compiledclass.Properties.Count() {
        s p=%compiledclass.Properties.GetAt(i), s=p.Storable
        s prop(s)=prop(s)_$e(",",prop(s)]"")_p.Name
    d %code.WriteLine($c(9)_"s stor="""_prop(1)_"""")
    d %code.WriteLine($c(9)_"q $s(all:"""_prop(0)_$e(",",prop(0)]"")_"""_stor,1:stor)")
    q $$$OK


For example

Class My.Person Extends (%Persistent, DC.ClassInfo)
Property Name As %String;
Property Age As %Integer;

Putting all together

write ##class(My.Person).PropNames() --> Age,Name
write ##class(My.Person).PropNames(1) --> %%OID,%Concurrency,Age,Name

// That way, in your application, you can easily check,
// if a property exists or not
if $length(##class(My.Person).PropNames(), ",", "TestProp") -->0
if $length(##class(My.Person).PropNames(), ",", "Age") -->1