MessageTemplateBase.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using Edge.Core.Parser.BinaryParser.Attributes;
  2. using Edge.Core.Parser.BinaryParser.Util;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace Edge.Core.Parser.BinaryParser.MessageEntity
  11. {
  12. public abstract class MessageTemplateBase : MessageBase
  13. {
  14. protected MessageTemplateBase() { }
  15. /// <summary>
  16. /// Validate the element object against on some high level validation rules.
  17. /// </summary>
  18. /// <returns>If an exception returned by overriden element class, the validation will be indicated as fail, the exception will be re-throw out</returns>
  19. public virtual Exception Validate() { return null; }
  20. /// <summary>
  21. /// Simply Xml-serialize the object to get the log string.
  22. /// </summary>
  23. /// <returns>xml format string</returns>
  24. public override string ToLogString()
  25. {
  26. var unformattedLogs = new List<string>();
  27. try
  28. {
  29. this.ToLogStringHelper(this, unformattedLogs, " ");
  30. }
  31. catch (Exception ex)
  32. {
  33. unformattedLogs.Add("Exception occured when generating the LogString for current message, exception detail: " + ex);
  34. }
  35. if (unformattedLogs.Any())
  36. return unformattedLogs.Aggregate((p, n) => p + System.Environment.NewLine + n) + System.Environment.NewLine + "------------";
  37. return "";
  38. }
  39. private List<string> ToLogStringHelper(object elementInstance, List<string> propertyNameValueString, string prefix)
  40. {
  41. int originalCount = propertyNameValueString.Count;
  42. // Only processing WayneAttribute marked properties, and order by its Index in DESC
  43. var targetPropertyList = elementInstance.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
  44. .Where(p => p.GetCustomAttributes(typeof(AttributeBase), true).Length > 0)
  45. .OrderBy(info => ((AttributeBase)(info.GetCustomAttributes(typeof(AttributeBase), true)[0])).Index);
  46. var plainPropertyLogStrAccumulator = string.Empty;
  47. foreach (var propertyInfo in targetPropertyList)
  48. {
  49. // normal plain Property
  50. if (propertyInfo.GetCustomAttributes(typeof(EnumerableFormatAttribute), true).Length == 0)
  51. {
  52. var value = propertyInfo.GetValue(elementInstance, null);
  53. string hexStr = "";
  54. if (value is int)
  55. {
  56. hexStr = "0x" + ((int)value).ToHexLogString();// ToString("X").PadLeft(2, '0');
  57. }
  58. else if (value is byte)
  59. {
  60. hexStr = "0x"
  61. + BitConverter.ToInt32(new byte[] { (byte)value, 0, 0, 0 }, 0).ToString("X").PadLeft(2, '0');
  62. }
  63. plainPropertyLogStrAccumulator += propertyInfo.Name + ": " +
  64. value.ToString() + "(" + hexStr + "), ";
  65. }
  66. // the IList Property
  67. else
  68. {
  69. var listFormat =
  70. (EnumerableFormatAttribute)propertyInfo.GetCustomAttributes(typeof(EnumerableFormatAttribute), true)[0];
  71. var list = (IList)propertyInfo.GetValue(elementInstance, null);
  72. if (list != null)
  73. {
  74. var genericArg = list.GetType().GetGenericArguments()[0];
  75. propertyNameValueString.Add(prefix + "*(List)" + propertyInfo.Name + "-->");
  76. string primitivePropertyStr = string.Empty;
  77. for (int i = 0; i <= list.Count - 1; i++)
  78. {
  79. if (genericArg.IsPrimitive)
  80. {
  81. primitivePropertyStr += "0x" + int.Parse(list[i].ToString()).ToString("X").PadLeft(2, '0') + " ";
  82. }
  83. else
  84. {
  85. this.ToLogStringHelper(list[i], propertyNameValueString, prefix + " [" + i + "]");
  86. if (list[i] is IFormattable) propertyNameValueString.Add(prefix + ((list[i] as IFormattable)).ToString(null, null));
  87. }
  88. }
  89. propertyNameValueString.Add(primitivePropertyStr);
  90. propertyNameValueString.Add(prefix + "*(List)" + propertyInfo.Name + "<--");
  91. }
  92. }
  93. }
  94. if (!string.IsNullOrEmpty(plainPropertyLogStrAccumulator))
  95. {
  96. // Add prefix and remove the last ", "
  97. plainPropertyLogStrAccumulator = prefix + plainPropertyLogStrAccumulator.Remove(plainPropertyLogStrAccumulator.Length - 2);
  98. propertyNameValueString.Insert(originalCount, plainPropertyLogStrAccumulator);
  99. }
  100. return propertyNameValueString;
  101. }
  102. }
  103. }