private Object ParseBitContent(BitContentConfig bcc, byte[] bytesValue) { Object bitContent = Activator.CreateInstance(bcc.ClassType); int i = 0; String[] locMsgs = new String[] { "BitContent element (id=" + bcc.Id + ") and field (name=", null, ")" }; foreach (BitContentFieldConfig bcfc in bcc.Fields) { Object propValue = null; if (bcfc.Tlv != null || !String.IsNullOrEmpty(bcfc.TlvTagName)) { int len = bytesValue.Length - i; byte[] bytes = new byte[len]; Array.Copy(bytesValue, i, bytes, 0, len); locMsgs[1] = bcfc.PropertyInfo.Name; int tlvLength = 0; propValue = ParseTlv(bcfc.PropertyInfo.PropertyType, bcfc.Tlv, bcfc.TlvTagName, bcfc.TlvLengthBytes, bcfc.TlvClassType, bytes, String.Join("", locMsgs), true, ref tlvLength); i += tlvLength; } else { int len = (i + bcfc.Length > bytesValue.Length) ? (bytesValue.Length - i) : bcfc.Length; if (len <= 0) { break; } byte[] bytes = new byte[len]; Array.Copy(bytesValue, i, bytes, 0, len); String val = MessageUtility.AsciiArrayToString(bytes); if (val != null) { if (bcfc.IsTrim) { val = val.Trim(); } propValue = val; } else { Type bcfType = bcfc.PropertyInfo.PropertyType; propValue = Util.GetAssignableValue(bcfType, bytes); if (propValue == null) { throw new MessageParserException("Cannot parse BitContent field named " + bcfc.PropertyInfo.Name + ". Check configuration for BitContent element (id=" + bcc.Id + ")"); } } i += bcfc.Length; } bcfc.PropertyInfo.SetValue(bitContent, propValue, null); if (i >= bytesValue.Length) { break; } } return(bitContent); }
private byte[] CompileBitContent(BitContentConfig cfg, Object obj) { if (cfg.ClassType != obj.GetType()) { throw new MessageCompilerException("Cannot compile a BitContent field. Not match target type of object. The passed " + "object has type of " + obj.GetType().FullName + " whereas the BitContent requires type of " + cfg.ClassType.FullName + ". Please check configuration for BitContent (id=" + cfg.Id); } String[] locMsgs = new String[] { "BitContent element (id=" + cfg.Id + ") and field (name=", null, ")" }; List <byte> bytes = new List <byte>(); foreach (BitContentFieldConfig ccfg in cfg.Fields) { Object propVal = ccfg.PropertyInfo.GetValue(obj, null); if (propVal == null) { if (!ccfg.IsOptional) { for (int i = 0; i < ccfg.Length; i++) { bytes.Add(ccfg.NullChar); } } } else if (propVal.GetType() == typeof(byte[])) { bytes.AddRange((byte[])propVal); } else if (ccfg.Tlv != null || !String.IsNullOrEmpty(ccfg.TlvTagName)) { locMsgs[1] = ccfg.PropertyInfo.Name; byte[] val = CompileTlv(ccfg.Tlv, ccfg.TlvTagName, ccfg.TlvLengthBytes, propVal, String.Join("", locMsgs)); bytes.AddRange(val); } else { byte[] value = MessageUtility.StringToAsciiArray(propVal.ToString()); if (value.Length > ccfg.Length) { locMsgs[1] = ccfg.PropertyInfo.Name; throw new MessageCompilerException( "Cannot compile a BitContent field. The length of field's value is too big more than " + ccfg.Length + " chars. Check configuration " + String.Join("", locMsgs)); } int i = value.Length; if (ccfg.Align == "right") { for (; i < ccfg.Length; i++) { bytes.Add(ccfg.PadChar); } } bytes.AddRange(value); if (ccfg.Align != "right") { for (; i < ccfg.Length; i++) { bytes.Add(ccfg.PadChar); } } } } return(bytes.ToArray()); }