سؤال

Something like this:

##class(MyApp.MyClass).%HasProperty("SomeProperty").  

I looked into doing something like this:

set classDefinition = ##class(%Dictionary.CompiledClass).%OpenId(%class.Name)

and then looping through the Properties, however, I need to be able to use any class, not just %class

هل كانت مفيدة؟

المحلول

For a simple OO approach, you can use the following API:

Set tPropExists = ##class(%Dictionary.CompiledProperty).IDKEYExists("SomeClass","SomeProperty")

This should have much less runtime cost than loading the class definition data and looping over its properties (and thus loading the data for those properties as well).

If you still want to create a %HasProperty() helper method for your application classes, you can use the following base method (assuming you are on Cache 2010.2 or higher - I believe the $this special variable and the $classname() function were added in 2010.2, but that may have been in 2010.1.):

ClassMethod %HasProperty(pPropName As %String = "") As %Boolean
{
  Set tHasProp = 0
  If (pPropName '= "") {
    Set tHasProp = ##class(%Dictionary.CompiledProperty).IDKEYExists($classname($this),pPropName)
  }
  Quit tHasProp
}

نصائح أخرى

You also might want to use a generator method (one of the really nice features in Cache objects) if run-time speed is important to you.

For example:

Method PropertyExists(Name) As %Boolean [ CodeMode = generator, ProcedureBlock = 1, ServerOnly = 1 ]
{
    Set %code=0
    S ClassDef=##class(%Dictionary.CompiledClass).%OpenId(%class)
    i '$IsObject(ClassDef)  $$$GENERATE(" Q 0")  Q $$$OK  
    I '$IsObject(ClassDef.Properties) $$$GENERATE(" Q 0")  Q $$$OK  
    S Key=""  F  S Key=ClassDef.Properties.Next(Key)  Q:Key=""  D
    . S CompiledProperty=ClassDef.Properties.GetAt(Key)
    . $$$GENERATE(" I Name="""_CompiledProperty.Name_""" Q 1" )
    $$$GENERATE(" Q 0")
    q $$$OK
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top