S7StringAttribute.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System;
  2. namespace S7.Net.Types
  3. {
  4. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
  5. public sealed class S7StringAttribute : Attribute
  6. {
  7. private readonly S7StringType type;
  8. private readonly int reservedLength;
  9. /// <summary>
  10. /// Initializes a new instance of the <see cref="S7StringAttribute"/> class.
  11. /// </summary>
  12. /// <param name="type">The string type.</param>
  13. /// <param name="reservedLength">Reserved length of the string in characters.</param>
  14. /// <exception cref="ArgumentException">Please use a valid value for the string type</exception>
  15. public S7StringAttribute(S7StringType type, int reservedLength)
  16. {
  17. if (!Enum.IsDefined(typeof(S7StringType), type))
  18. throw new ArgumentException("Please use a valid value for the string type");
  19. this.type = type;
  20. this.reservedLength = reservedLength;
  21. }
  22. /// <summary>
  23. /// Gets the type of the string.
  24. /// </summary>
  25. /// <value>
  26. /// The string type.
  27. /// </value>
  28. public S7StringType Type => type;
  29. /// <summary>
  30. /// Gets the reserved length of the string in characters.
  31. /// </summary>
  32. /// <value>
  33. /// The reserved length of the string in characters.
  34. /// </value>
  35. public int ReservedLength => reservedLength;
  36. /// <summary>
  37. /// Gets the reserved length in bytes.
  38. /// </summary>
  39. /// <value>
  40. /// The reserved length in bytes.
  41. /// </value>
  42. public int ReservedLengthInBytes => type == S7StringType.S7String ? reservedLength + 2 : (reservedLength * 2) + 4;
  43. }
  44. /// <summary>
  45. /// String type.
  46. /// </summary>
  47. public enum S7StringType
  48. {
  49. /// <summary>
  50. /// ASCII string.
  51. /// </summary>
  52. S7String = VarType.S7String,
  53. /// <summary>
  54. /// Unicode string.
  55. /// </summary>
  56. S7WString = VarType.S7WString
  57. }
  58. }