public IsoValue(IsoType t, object val, int len, ICustomField custom = null) { Type = t; Value = val; Length = len; Encoder = custom; if (Length == 0 && t.NeedsLength()) { throw new ArgumentException($"Length must be greater than zero for type {t} (value '{val}')"); } switch (t) { case IsoType.LLVAR: case IsoType.LLLVAR: case IsoType.LLLLVAR: if (len == 0) { Length = Encoder?.EncodeField(val).Length ?? val.ToString().Length; } if (t == IsoType.LLVAR && Length > 99) { throw new ArgumentException("LLVAR can only hold values up to 99 chars"); } if (t == IsoType.LLLVAR && Length > 999) { throw new ArgumentException("LLLVAR can only hold values up to 999 chars"); } if (t == IsoType.LLLLVAR && Length > 9999) { throw new ArgumentException("LLLLVAR can only hold values up to 9999 chars"); } break; case IsoType.LLBIN: case IsoType.LLLBIN: case IsoType.LLLLBIN: if (len == 0) { if (Encoder == null) { var obj = val; Length = ((byte[])obj).Length; } else if (Encoder is ICustomBinaryField) { var customBinaryField = (ICustomBinaryField)custom; if (customBinaryField != null) { Length = customBinaryField.EncodeBinaryField(Value).Length; } } else { Length = Encoder.EncodeField(Value).Length; } Length = Encoder?.EncodeField(Value).Length ?? ((sbyte[])val).Length; } if (t == IsoType.LLBIN && Length > 99) { throw new ArgumentException("LLBIN can only hold values up to 99 chars"); } if (t == IsoType.LLLBIN && Length > 999) { throw new ArgumentException("LLLBIN can only hold values up to 999 chars"); } if (t == IsoType.LLLLBIN && Length > 9999) { throw new ArgumentException("LLLLBIN can only hold values up to 9999 chars"); } break; } }
public IsoValue(IsoType t, object value, ICustomField custom = null) { if (t.NeedsLength()) { throw new ArgumentException("Fixed-value types must use constructor that specifies length"); } Encoder = custom; Type = t; Value = value; switch (Type) { case IsoType.LLVAR: case IsoType.LLLVAR: case IsoType.LLLLVAR: if (Encoder == null) { Length = value.ToString().Length; } else { var enc = Encoder.EncodeField(value) ?? (value?.ToString() ?? string.Empty); Length = enc.Length; } if (t == IsoType.LLVAR && Length > 99) { throw new ArgumentException("LLVAR can only hold values up to 99 chars"); } if (t == IsoType.LLLVAR && Length > 999) { throw new ArgumentException("LLLVAR can only hold values up to 999 chars"); } if (t == IsoType.LLLLVAR && Length > 9999) { throw new ArgumentException("LLLLVAR can only hold values up to 9999 chars"); } break; case IsoType.LLBIN: case IsoType.LLLBIN: case IsoType.LLLLBIN: if (Encoder == null) { if (value.GetType() == typeof(sbyte[])) { var obj = value; Length = ((sbyte[])obj).Length; } else { Length = value.ToString().Length / 2 + value.ToString().Length % 2; } } else if (Encoder is ICustomBinaryField) { Length = ((ICustomBinaryField)Encoder).EncodeBinaryField(value).Length; } else { var enc = Encoder.EncodeField(value) ?? (value?.ToString() ?? string.Empty); Length = enc.Length; } if (t == IsoType.LLBIN && Length > 99) { throw new ArgumentException("LLBIN can only hold values up to 99 chars"); } if (t == IsoType.LLLBIN && Length > 999) { throw new ArgumentException("LLLBIN can only hold values up to 999 chars"); } if (t == IsoType.LLLLBIN && Length > 9999) { throw new ArgumentException("LLLLBIN can only hold values up to 9999 chars"); } break; default: Length = Type.Length(); break; } }