private BitContentConfig ParseBitContent(Type bitContentType, string sourceMsg) { if (MessageConfigs.BitContents.ContainsKey(bitContentType.FullName)) { if (MessageConfigs.BitContents[bitContentType.FullName].ClassType != bitContentType) { throw new ConfigParserException("There is already BitContent whose type of " + bitContentType.FullName + ". " + sourceMsg); } return(MessageConfigs.BitContents[bitContentType.FullName]); } BitContentConfig cfg = new BitContentConfig(); cfg.Id = bitContentType.FullName; cfg.ClassType = bitContentType; ParseBitContentFields(bitContentType, cfg, sourceMsg); MessageConfigs.BitContents.Add(cfg.Id, cfg); return(cfg); }
private void ParseBitContentFields(Type bitContentType, BitContentConfig cfg, string sourceMsg) { IList <BitContentFieldAttribute> fields = new List <BitContentFieldAttribute>(); IList <PropertyInfo> properties = new List <PropertyInfo>(); foreach (var propInfo in bitContentType.GetProperties()) { var attr = GetAttribute <BitContentFieldAttribute>(propInfo); if (attr == null) { continue; } fields.Add(attr); properties.Add(propInfo); } int fieldCount = fields.Count; if (fieldCount <= 0) { throw new ConfigParserException("There is no property which defines BitContentFieldAttribute in " + bitContentType.FullName + " class. " + sourceMsg); } sourceMsg = "Check attribute for {0} property of " + bitContentType.FullName + " class."; for (int i = 0; i < fieldCount; i++) { var ccfg = new BitContentFieldConfig(); var attr = fields[i]; var propInfo = properties[i]; string srcMsg = String.Format(sourceMsg, propInfo.Name); CheckNonNegativeValue(attr, "Length", srcMsg); attr.TlvTagName = Trim(attr.TlvTagName); if (attr.Length <= 0 && IsEmpty(attr.Tlv) && IsEmpty(attr.TlvTagName)) { throw new ConfigParserException( "Length must be set to a positive value if Tlv and TlvTagName are not specified. " + srcMsg); } if (Encoding.UTF8.GetByteCount(attr.PadChar.ToString()) != 1) { throw new ConfigParserException("PadChar must be a valid ASCII character. " + srcMsg); } if (IsEmpty(attr.Align)) { throw new ConfigParserException("Invalid Align value, it must be 'Left' or 'Right'. " + srcMsg); } if (Encoding.UTF8.GetByteCount(attr.NullChar.ToString()) != 1) { throw new ConfigParserException("NullChar must be a valid ASCII character. " + srcMsg); } if (attr.IsOptional && i < fieldCount - 1) { throw new ConfigParserException("IsOptional may only be true for the last field of a BitContent class. " + srcMsg); } if (!IsEmpty(attr.Tlv)) { ccfg.Tlv = ParseTlv(attr.Tlv, srcMsg); } ccfg.PropertyInfo = propInfo; ccfg.Length = attr.Length; ccfg.PadChar = MessageUtility.CharToByte(attr.PadChar); ccfg.Align = attr.Align.ToString().ToLower(); ccfg.NullChar = MessageUtility.CharToByte(attr.NullChar); ccfg.IsTrim = attr.IsTrim; ccfg.IsOptional = attr.IsOptional; ccfg.TlvTagName = attr.TlvTagName; ccfg.TlvLengthBytes = attr.TlvLengthBytes; ccfg.TlvClassType = attr.Tlv; cfg.Fields.Add(ccfg); } }