AssertBase.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using XGame.Framework.Assertions;
  2. using System;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.Text;
  6. namespace XGame.Framework
  7. {
  8. //[DebuggerStepThrough]
  9. public static partial class Assert
  10. {
  11. //public static bool raiseExceptions = false;
  12. static StringBuilder userMsgStringBuilder = new StringBuilder(1024);
  13. static void Fail(string message, string format, params object[] args)
  14. {
  15. userMsgStringBuilder.Length = 0;
  16. string userMessage = string.Empty;
  17. if (!string.IsNullOrEmpty(format))
  18. userMessage = userMsgStringBuilder.AppendFormat(format, args).ToString();
  19. ThrowException(message, userMessage);
  20. LogDisk(message, userMessage);
  21. }
  22. static void Fail<T>(string message, T e) where T : Exception
  23. {
  24. ThrowException(message, e);
  25. LogDisk(message, e.Message);
  26. }
  27. [Conditional(MacroDefine.UNITY_EDITOR), Conditional(MacroDefine.UNITY_STANDALONE_WIN)]
  28. private static void ThrowException(string message, string userMessage)
  29. {
  30. throw new AssertionException(message, userMessage);
  31. }
  32. [Conditional(MacroDefine.UNITY_EDITOR), Conditional(MacroDefine.UNITY_STANDALONE_WIN)]
  33. private static void ThrowException<T>(string message, T e) where T : Exception
  34. {
  35. throw (T)Activator.CreateInstance(typeof(T), $"\n【XGame Assert】{e.Message}\n{message}", new Exception("<----"));
  36. }
  37. private static void LogDisk(string message, string userMessage)
  38. {
  39. Log.Error(string.Format("【XGame Assert】{0} {1}", userMessage, message));
  40. }
  41. [EditorBrowsable(EditorBrowsableState.Never)]
  42. [Obsolete("Assert.Equals should not be used for Assertions", true)]
  43. public new static bool Equals(object obj1, object obj2)
  44. {
  45. throw new InvalidOperationException("Assert.Equals should not be used for Assertions");
  46. }
  47. [EditorBrowsable(EditorBrowsableState.Never)]
  48. [Obsolete("Assert.ReferenceEquals should not be used for Assertions", true)]
  49. public new static bool ReferenceEquals(object obj1, object obj2)
  50. {
  51. throw new InvalidOperationException("Assert.ReferenceEquals should not be used for Assertions");
  52. }
  53. }
  54. }