Example #1
0
        private TlvConfig ParseTlv(Type tlvType, string sourceMsg)
        {
            if (MessageConfigs.Tlvs.ContainsKey(tlvType.FullName))
            {
                if (MessageConfigs.Tlvs[tlvType.FullName].ClassType != tlvType)
                {
                    throw new ConfigParserException("There is already Tlv whose type of " + tlvType.FullName + ". " + sourceMsg);
                }
                return(MessageConfigs.Tlvs[tlvType.FullName]);
            }

            var attr = GetAttribute <TlvAttribute>(tlvType);

            if (attr == null)
            {
                throw new ConfigParserException(tlvType.FullName + " class doesn't have TlvAttribute. " + sourceMsg);
            }
            CheckPositiveValue(attr, "LengthBytes", "Check attribute for " + tlvType.FullName + " class.");

            TlvConfig cfg = new TlvConfig();

            cfg.Id          = tlvType.FullName;
            cfg.LengthBytes = attr.LengthBytes;
            cfg.ClassType   = tlvType;
            ParseTlvTags(tlvType, cfg);

            MessageConfigs.Tlvs.Add(cfg.Id, cfg);
            return(cfg);
        }
Example #2
0
        private void ParseTlvTags(Type tlvType, TlvConfig cfg)
        {
            int fieldCount = 0;

            foreach (var propInfo in tlvType.GetProperties())
            {
                var attr = GetAttribute <TlvTagAttribute>(propInfo);
                if (attr == null)
                {
                    continue;
                }
                fieldCount++;

                TlvTagConfig ccfg = new TlvTagConfig();
                ccfg.Name = propInfo.Name;

                ccfg.Splitter = null;
                if (attr.Type == TlvTagType.Array)
                {
                    ccfg.Splitter = Trim(attr.Splitter);
                    if (ccfg.Splitter == String.Empty)
                    {
                        ccfg.Splitter = ";";
                    }
                }

                if (attr.Type == TlvTagType.BitContent)
                {
                    ccfg.BitContent = ParseBitContent(propInfo.PropertyType, "Check attribute for " + propInfo.Name + " of "
                                                      + tlvType.FullName + " class.");
                }

                cfg.AddTag(ccfg);
            }

            if (fieldCount <= 0)
            {
                throw new ConfigParserException(tlvType.FullName + " class defines TlvAttribute but there is no property which "
                                                + "defines TlvTagAttribute");
            }
        }