protected string GetFixedFieldValue(string name) { FixedLengthField field = this.GetFixedField(name); if (field == null) { throw new Exception("未定义定长字段" + name); } return(field.Value); }
// 设置某个定长字段的值 protected void SetFixedFieldValue(string name, string value) { FixedLengthField field = this.GetFixedField(name); if (field == null) { throw new Exception("未定义定长字段" + name); } field.Value = value; }
// 将对象转换字符串命令 public virtual string ToText() { Debug.Assert(String.IsNullOrEmpty(this.CommandIdentifier) == false, "命令指示符未赋值"); StringBuilder text = new StringBuilder(this.CommandIdentifier); if (this.FixedLengthFields != null) { //foreach (FixedLengthField field in this.FixedLengthFields) for (int i = 0; i < this.FixedLengthFields.Count; i++) { FixedLengthField field = this.FixedLengthFields[i]; if (field.Value == null || field.Value.Length != field.Length) { throw new Exception("定长字段[" + field.Name + "]的值为null或者长度不符合定义"); } text.Append(field.Value); } } if (this.VariableLengthFields != null && this.VariableLengthFields.Count > 0) { //foreach (VariableLengthField field in this.VariableLengthFields) for (int i = 0; i < this.VariableLengthFields.Count; i++) { VariableLengthField field = this.VariableLengthFields[i]; if (field.Value != null) { text.Append(field.ID + field.Value + SIPConst.FIELD_TERMINATOR); } } } string result = text.ToString(); // 去掉字符串最后一个| if (string.IsNullOrEmpty(result) == false) { if (result.Substring(result.Length - 1) == SIPConst.FIELD_TERMINATOR) { result = result.Substring(0, result.Length - 1); } } return(result); }