コード例 #1
0
        private void ParseMessageFields(Type modelType, MessageConfig cfg, string sourceMsg)
        {
            IDictionary <int, MessageFieldConfig> configs = new Dictionary <int, MessageFieldConfig>();
            List <FieldAttribute> attrs = new List <FieldAttribute>();

            attrs.AddRange(GetClassFieldAttributes(modelType));
            List <PropertyInfo> propInfos = new List <PropertyInfo>();

            for (int i = 0; i < attrs.Count; i++)
            {
                propInfos.Add(null);
            }
            foreach (var propInfo in modelType.GetProperties())
            {
                var attr = GetAttribute <FieldAttribute>(propInfo);
                if (attr == null)
                {
                    continue;
                }
                attrs.Add(attr);
                propInfos.Add(propInfo);
            }

            for (int i = 0; i < attrs.Count; i++)
            {
                var    attr   = attrs[i];
                string srcMsg = sourceMsg;
                if (propInfos[i] != null)
                {
                    srcMsg = "Check attribute for " + propInfos[i].Name + " property of " + modelType.FullName + " class.";
                }

                MessageFieldConfig ccfg = new MessageFieldConfig();
                ccfg.FieldType = attr.FieldClass;
                CheckPositiveValue(attr, "Seq", srcMsg);
                ccfg.Seq = attr.Seq;

                if (attr.FieldType == FieldType.Null)
                {
                    ccfg.Length = 0;
                }
                else if (attr.FieldType == FieldType.BitMap)
                {
                    CheckPositiveValue(attr, "Length", srcMsg);
                    ccfg.Length = ((BitMapFieldAttribute)attr).Length;
                }
                else
                {
                    var attr2 = (PropertyFieldAttribute)attr;

                    if (!(attr2.Length > 0 ^ attr2.LengthHeader > 0))
                    {
                        throw new ConfigParserException(
                                  "Either Length or LengthHeader property of PropertyFieldAttribute must be greater than 0 but NOT BOTH. "
                                  + srcMsg);
                    }
                    if (attr2.Length > 0)
                    {
                        ccfg.Length       = attr2.Length;
                        ccfg.LengthHeader = -1;
                    }
                    else
                    {
                        ccfg.Length       = -1;
                        ccfg.LengthHeader = attr2.LengthHeader;
                    }

                    ccfg.FromRequest = attr2.FromRequest;

                    if (attr2.FieldDelegateClass != null)
                    {
                        CheckRequired(attr2, "FieldDelegateMethod", srcMsg);
                        ccfg.GetFieldBytesFunc = ParseDelegate(typeof(GetFieldBytes <>), attr2.FieldDelegateClass,
                                                               attr2.FieldDelegateMethod, srcMsg);
                    }
                }

                if (configs.ContainsKey(attr.Seq))
                {
                    throw new ConfigParserException("There are more than one message field whose the same Seq value (Seq=" + attr.Seq
                                                    + "). " + sourceMsg);
                }
                configs.Add(attr.Seq, ccfg);
            }

            /*** Sorts the fields based on their sequence number. It ensures we get the field
             *   from the lowest sequence until the highest one consecutively when they are iterated. ***/
            List <int> keys = configs.Keys.ToList();

            keys.Sort();
            for (int i = 0; i < keys.Count; i++)
            {
                int iSeq = keys[i];
                cfg.Fields.Add(iSeq, configs[iSeq]);
            }
        }
コード例 #2
0
        protected static void HookPropertyToField(ModelPropertyConfig propertyConfig, MessageFieldConfig fieldConfig,
                                                  string fieldType)
        {
            propertyConfig.FieldBit = fieldConfig;
            ParameterModifier[] pm = null;// new ParameterModifier[] { };
            switch (fieldType.ToLower())
            {
            case "string":
                propertyConfig.GetValueFromBytes    = fieldConfig.FieldType.GetProperty("StringValue");
                propertyConfig.SetValueFromProperty = fieldConfig.FieldType.GetMethod("SetValue",
                                                                                      BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance,
                                                                                      null, new Type[] { typeof(String) }, pm);
                //propertyConfig.SetValueFromProperty = fieldConfig.FieldType.GetMethod("SetValue", new Type[] { typeof(String) });
                break;

            case "int":
                propertyConfig.GetValueFromBytes    = fieldConfig.FieldType.GetProperty("IntValue");
                propertyConfig.SetValueFromProperty = fieldConfig.FieldType.GetMethod("SetValue",
                                                                                      BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance,
                                                                                      null, new Type[] { typeof(int) }, pm);
                //propertyConfig.SetValueFromProperty = fieldConfig.FieldType.GetMethod("SetValue", new Type[] { typeof(int) });
                break;

            case "decimal":
                propertyConfig.GetValueFromBytes    = fieldConfig.FieldType.GetProperty("DecimalValue");
                propertyConfig.SetValueFromProperty = fieldConfig.FieldType.GetMethod("SetValue",
                                                                                      BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance,
                                                                                      null, new Type[] { typeof(decimal) }, pm);
                //propertyConfig.SetValueFromProperty = fieldConfig.FieldType.GetMethod("SetValue", new Type[] { typeof(decimal) });
                break;

            case "bytes":
                propertyConfig.GetValueFromBytes = fieldConfig.FieldType.GetProperty("BytesValue",
                                                                                     BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
                propertyConfig.SetValueFromProperty = fieldConfig.FieldType.GetMethod("SetValue",
                                                                                      BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance,
                                                                                      null, new Type[] { typeof(byte[]) }, pm);
                //propertyConfig.SetValueFromProperty = fieldConfig.FieldType.GetMethod("SetValue", new Type[] { typeof(byte[]) });
                break;
            }
        }