Written by

Question Pedro Lopes · Feb 3, 2023

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?

Comments

Cristiano Silva · Feb 3, 2023

Hi Pedro,

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

An example:

SELECTNameFROM %Dictionary.PropertyDefinition
WHEREparent = "mypackage.MyClassName"

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

0
Pedro Lopes  Feb 10, 2023 to Cristiano Silva

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

0
Lorenzo Scalese · Feb 3, 2023

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('class.name','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
0
Julius Kavay · Feb 3, 2023

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 onlyClassMethod 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 notif$length(##class(My.Person).PropNames(), ",", "TestProp") -->0if$length(##class(My.Person).PropNames(), ",", "Age") -->1
0