Extensions.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Mirror
  4. {
  5. public static class Extensions
  6. {
  7. // string.GetHashCode is not guaranteed to be the same on all machines, but
  8. // we need one that is the same on all machines. simple and stupid:
  9. public static int GetStableHashCode(this string text)
  10. {
  11. unchecked
  12. {
  13. int hash = 23;
  14. foreach (char c in text)
  15. hash = hash * 31 + c;
  16. return hash;
  17. }
  18. }
  19. // previously in DotnetCompatibility.cs
  20. // leftover from the UNET days. supposedly for windows store?
  21. internal static string GetMethodName(this Delegate func)
  22. {
  23. #if NETFX_CORE
  24. return func.GetMethodInfo().Name;
  25. #else
  26. return func.Method.Name;
  27. #endif
  28. }
  29. // helper function to copy to List<T>
  30. // C# only provides CopyTo(T[])
  31. public static void CopyTo<T>(this IEnumerable<T> source, List<T> destination)
  32. {
  33. // foreach allocates. use AddRange.
  34. destination.AddRange(source);
  35. }
  36. }
  37. }