我正在将Fhir-net-api与DSTU2一起使用,以将JSON对象解析为C#模型。一切正常,除了我无法访问资源类型“过程”的Reason元素。例如,我使用FhirParser将以下JSON对象解析为Procedure模型:{"resourceType": "Procedure","identifier": [ { "system": "https://mrd2.melanoma.org.au/fhir", "value": "100200199664802" }],"subject": { "reference": "Patient/10101000001733" },"status": "completed","category": { "coding": [ { "system": "https://mrd2.melanoma.org.au/fhir/RootType", "code": "3004" } ], "text": "Primary Surgery"},"bodySite": [ { "coding": [ { "system": "http://snomed.info/sct", "code": "7771000" } ], "text": "Left Forearm, Anterior" }],"reasonReference": { "reference": "/Condition/10106000001807" },"performedDateTime": "1968-03-11","report": [ { "reference": "/DiagnosticReport/100200199664828" } ]}并且生成的对象具有以下条目(节选):Procedure我可以很好地访问Report[0].Reference,但不能与Reason.Reference一起使用。 JSON对象中的数据是否错误?我已经看到,Reason是Hl7.Fhir.Model.Element类型的,而Report是Hl7.Fhir.Model.ResourceReference类型的。有没有一种方法可以将Reason更改为Hl7.Fhir.Model.ResourceReference,然后访问Reference元素?不胜感激任何提示。谢谢。问候,摇摇欲坠 最佳答案 如您所见,reasonReference的类型为Model.Element,而report的类型为ResourceReference。这种差异起源于FHIR specification for Procedure中这些元素的定义,其中report固定为类型Reference,但是reason(或更确切地说是reason[x])可以是CodeableConcept或。当元素可以是多种类型时(我们称其为“选择元素”,您可以识别它们,因为它们的名称以规范中的Reference结尾),我们创建了一个C#成员,其类型为[x]( Reference和CodeableConcept的基类)。现在,根据您刚解析或接收的实例,Model.Element成员的内容可以是两种类型之一。因此,您必须检入代码:if(Reports[0].reason is ResourceReference){ var reference = (ResourceReference)Reports[0].reason; //handle the case where this is a reference //doing reference.Reference will now work as expected}else if(Reports[0].reason is CodeableConcept){ var concept = (CodeableConcept)Reports[0].reason; //handle the case where this is a codeable concept}else{ // Oops! Should not happen unless the standard has changed}当然,如果您确定只可以接收reason为ResourceReference的实例,则可以直接进行强制转换:var myReference = (ResourceReference)Reports[0].Reference;// myReference.Reference and myReference.Display will now work关于c# - 无法在FHIR模型中访问Procedure.reason,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34302188/ 10-11 17:38