Search code examples
hl7-fhir

Iterate through a FHIR element binding


I'm trying to iterate through a FHIR capability statement to pull down a list of all the questions and values sets that are supported by the server.

I have a downloaded FHIR capability statement, and subsequent structure definition and within the structure definition I am iterating through each snapshot element.

Some of the elements have a binding, which I can see is correctly populated from the console and debug break points but I can't determine the correct way to access this in code.

For example, the immediate window is giving me the following information about the element.Binding.ValueSet:

element.Binding.ValueSet
{url: http://hl7.org/fhir/ValueSet/adverse-event-category} Children: {Hl7.Fhir.Model.ResourceReference.<get_Children>d__32} Display: null DisplayElement: null ElementId: null Extension: Count = 0 Identifier: null IsContainedReference: false NamedChildren: {Hl7.Fhir.Model.ResourceReference.<get_NamedChildren>d__34} Reference: "http://hl7.org/fhir/ValueSet/adverse-event-category" ReferenceElement: {Value="http://hl7.org/fhir/ValueSet/adverse-event-category"} Type: null TypeElement: null TypeName: "Reference" Url: {http://hl7.org/fhir/ValueSet/adverse-event-category} Results View: Expanding the Results View will enumerate the IEnumerable

But I can't access these "Properties", for example: element.Binding.ValueSet.Reference returns

element.Binding.ValueSet.Reference
error CS1061: 'DataType' does not contain a definition for 'Reference' and no accessible extension method 'Reference' accepting a first argument of type 'DataType' could be found (are you missing a using directive or an assembly reference?)

When I interrogate the type or properties I get the following:

element.Binding.ValueSet.GetType() (results abridged)
{Name = "ResourceReference" FullName = "Hl7.Fhir.Model.ResourceReference"}
Assembly: {Hl7.Fhir.Support.Poco, Version=4.3.0.0, Culture=neutral, PublicKeyToken=d706911480550fc3}
AssemblyQualifiedName: "Hl7.Fhir.Model.ResourceReference, Hl7.Fhir.Support.Poco, Version=4.3.0.0, Culture=neutral, PublicKeyToken=d706911480550fc3"
BaseType: {Name = "DataType" FullName = "Hl7.Fhir.Model.DataType"}
DeclaredNestedTypes: {System.Reflection.TypeInfo.<get_DeclaredNestedTypes>d__22}
DeclaredProperties: {System.Reflection.PropertyInfo[13]}
FullName: "Hl7.Fhir.Model.ResourceReference"
Module (System.Reflection.MemberInfo): {Hl7.Fhir.Support.Poco.dll} Module: {Hl7.Fhir.Support.Poco.dll} Name: "ResourceReference" Namespace: "Hl7.Fhir.Model" UnderlyingSystemType: {Name = "ResourceReference" FullName = "Hl7.Fhir.Model.ResourceReference"}

element.Binding.ValueSet.GetType().GetProperties()
{System.Reflection.PropertyInfo[14]}
[0]: {System.String TypeName}
[1]: {Hl7.Fhir.Model.FhirString ReferenceElement}
[2]: {System.String Reference}
[3]: {Hl7.Fhir.Model.FhirUri TypeElement}
[4]: {System.String Type}
[5]: {Hl7.Fhir.Model.Identifier Identifier}
[6]: {Hl7.Fhir.Model.FhirString DisplayElement}
[7]: {System.String Display}
[8]: {System.Collections.Generic.IEnumerable'1[Hl7.Fhir.Model.Base] Children}
[9]: {System.Collections.Generic.IEnumerable'1[Hl7.Fhir.Model.ElementValue]
NamedChildren} [10]: {System.Uri Url}
[11]: {Boolean IsContainedReference}
[12]: {System.String ElementId}
[13]: {System.Collections.Generic.List`1[Hl7.Fhir.Model.Extension] Extension}

I can convert this object to a list, it appears to be some enumerable of KVPs:

element.Binding.ValueSet.ToList()
Count = 1
[0]: {[reference, {Value=http://hl7.org/fhir/ValueSet/adverse-event-category}]}

But calling for the element.Binding.ValueSet.Keys or element.Binding.ValueSet.Values does not work either

What is the correct way to work with this object?


Solution

  • Assuming you are using FHIR STU3, element.Binding.ValueSet will be of the generic DataType which can mean any of the FHIR datatypes, since this element is a choice property and could be either of type Reference or Uri. The way to access the data inside is to cast the ValueSet property to ResourceReference or use as ResourceReference, after which you'll be able to access the reference properties.