private void ParseModelProperties(Type modelType, ModelConfig cfg) { string sourceMsg = "Check attribute for {0} property of " + modelType.FullName + " class."; foreach (var propInfo in modelType.GetProperties()) { var attr = GetAttribute <PropertyFieldAttribute>(propInfo); if (attr == null) { continue; } string srcMsg = string.Format(sourceMsg, propInfo.Name); CheckPositiveValue(attr, "Seq", srcMsg); CheckNonNegativeValue(attr, "FracDigits", srcMsg); attr.TlvTagName = Trim(attr.TlvTagName); CheckPositiveValue(attr, "TlvLengthBytes", srcMsg); if (IsEmpty(attr.PropertyType) && IsEmpty(attr.PropertyDelegateClass) && IsEmpty(attr.Tlv) && IsEmpty(attr.TlvTagName)) { throw new ConfigParserException( "Either PropertyType or PropertyDelegateClass or Tlv or TlvTagName property must be defined. " + srcMsg); } if (!IsEmpty(attr.PropertyDelegateClass)) { CheckRequired(attr, "PropertyDelegateMethod", srcMsg); } ModelPropertyConfig ccfg = new ModelPropertyConfig(); ccfg.PropertyInfo = propInfo; ccfg.FracDigits = attr.FracDigits; ccfg.GetPropertyValueFunc = ParseDelegate(typeof(GetPropertyValue <>), attr.PropertyDelegateClass, attr.PropertyDelegateMethod, srcMsg); ccfg.BitContent = attr.PropertyType != PropertyType.BitContent ? null : ParseBitContent(propInfo.PropertyType, srcMsg); ccfg.Tlv = attr.Tlv == null ? null : ParseTlv(attr.Tlv, srcMsg); ccfg.TlvTagName = attr.TlvTagName.ToUpper(); ccfg.TlvLengthBytes = attr.TlvLengthBytes; ccfg.TlvClassType = ccfg.Tlv == null ? attr.Tlv : null; var fieldBit = cfg.MessageCfg.Fields[attr.Seq]; //if property delegate is not ignored (because it's not bitcontent or Tlv and TlvTagName are not specified) then //the mapped field must also specify field delegate if (ccfg.BitContent == null && ccfg.Tlv == null && IsEmpty(ccfg.TlvTagName) && ccfg.GetPropertyValueFunc != null && fieldBit.GetFieldBytesFunc == null) { throw new ConfigParserException("There is a property defines PropertyDelegateClass but not define " + "FieldDelegateClass. " + srcMsg); } HookPropertyToField(ccfg, fieldBit, attr.PropertyType.ToString()); cfg.Properties.Add(attr.Seq, ccfg); } }
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; } }
public void AddOrSubtituteProperty(ModelPropertyConfig propCfg) { int i = -1; String propName = propCfg.PropertyInfo.Name; foreach (KeyValuePair <int, ModelPropertyConfig> kvp in Properties) { if (kvp.Value.PropertyInfo.Name == propName) { i = kvp.Key; break; } } if (i == -1) { Properties.Add(propCfg.FieldBit.Seq, propCfg); } else { Properties.Remove(i); Properties[propCfg.FieldBit.Seq] = propCfg; } }