internal void Encode(HL7Separators seps, System.Text.StringBuilder sb) { FieldInfo[] fields = GetType().GetFields(); sb.Append(GetType().Name); sb.Append(seps.FieldSeparator); foreach (FieldInfo field in fields) { if (this is MSH && field.Name.Equals("FieldSeparator")) { // A field separator was already added after the segment HL7Name, don't add another. } else if (this is MSH && field.Name.Equals("EncodingCharacters")) { // We want the encoding characters raw, not escaped. sb.Append(field.GetValue(this)); sb.Append(seps.FieldSeparator); } else { HL7Field.Encode(seps, sb, field.FieldType, field.GetValue(this)); sb.Append(seps.FieldSeparator); } } }
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); }
/// <summary> /// Parses each of the fields according to its declared identifier in the segment structure. /// Ignores any extra fields either in the received segment or in the structure. /// </summary> /// <param HL7Name="sep">Separators to use for repeating fields, components and subcomponents</param> /// <param HL7Name="fieldForm">Encoded segment split into fields, where fieldForm[0] is the segment name</param> /// <param HL7Name="segment">Empty object of desired segment structure</param> private static void PopulateSegment(HL7Separators sep, string[] fieldForm, HL7Segment segment) { FieldInfo[] fields = segment.GetType().GetFields(); // Subtract one from the number of fields in the received segment to avoid counting the segment name. int offset = 1; // Except when segment is MSH, then have no offset but the first field is the field separator. if (fieldForm[0].Equals("MSH")) { fields[0].SetValue(segment, "" + sep.FieldSeparator); offset = 0; } int n = Math.Min(fields.Length, fieldForm.Length - 1); for (int i = 1 - offset; i < n; i++) { object value = HL7Field.Parse(fields[i].FieldType, fieldForm[i + offset], sep); fields[i].SetValue(segment, value); } }