public static object Parse(Type type, string componentForm, HL7Separators sep) { object component; // A component can either be a string, or it will consist of sub-components. if (type.Equals(typeof(string))) { component = sep.Decode(componentForm); } else { FieldInfo[] subcomponents = type.GetFields(); string[] subcomponentForm = componentForm.Split(sep.SubcomponentSeparator); int n = Math.Min(subcomponents.Length, subcomponentForm.Length); // Invoke the constructor that takes no parameters component = type.GetConstructor(System.Type.EmptyTypes).Invoke(null); for (int i = 0; i < n; i++) { // Each subcomponent must be a string if (subcomponents[i].FieldType.Equals(typeof(string))) { subcomponents[i].SetValue(component, sep.Decode(subcomponentForm[i])); } else if (subcomponents[i].FieldType.Equals(typeof(HL7.Common.DataStructure.HD))) // For OBR segment PrincipalResultsInterpreter field we have assigningAuthority that needs to be set { object objHD = new HL7.Common.DataStructure.HD(); FieldInfo[] subcomponentsHD = objHD.GetType().GetFields(); for (int j = 0; j < subcomponentsHD.Length; j++) { if (subcomponentsHD[j].Name == "universalID") // Set the UniversalID { subcomponentsHD[j].SetValue(objHD, sep.Decode(subcomponentForm[i])); // subcomponents[i].SetValue(component, objHD); } } } else { throw new HL7ParseException(string.Format(ConstantsResource.SubcomponentNotString, subcomponents[i].GetType())); } } } return(component); }
public static object Parse(Type type, string fieldForm, HL7Separators sep) { object field; // A field can be an array of repeats, or a string identifier, or a data structure with components if (type.IsArray) { Type elementType = type.GetElementType(); string[] elementForm; if (fieldForm.IndexOf(sep.FieldRepeatSeparator) == -1 && fieldForm.IndexOf(sep.ComponentSeparator) > -1 && elementType.Equals(typeof(string))) { elementForm = fieldForm.Split(sep.ComponentSeparator); } else { elementForm = fieldForm.Split(sep.FieldRepeatSeparator); } Array array = Array.CreateInstance(elementType, elementForm.Length); for (int i = 0; i < elementForm.Length; i++) { array.SetValue(HL7Field.Parse(elementType, elementForm[i], sep), i); } field = array; } else if (type.Equals(typeof(string))) { field = sep.Decode(fieldForm); } else { FieldInfo[] components = type.GetFields(); string[] componentForm = fieldForm.Split(sep.ComponentSeparator); int n = Math.Min(components.Length, componentForm.Length); // Invoke the constructor that takes no parameters field = type.GetConstructor(System.Type.EmptyTypes).Invoke(null); for (int i = 0; i < n; i++) { object value = HL7Component.Parse(components[i].FieldType, componentForm[i], sep); components[i].SetValue(field, value); } } return(field); }