You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.7 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Net3_Proxy
  6. {
  7. internal static class TypeUtils
  8. {
  9. public static void ValidateType(Type type, string paramName)
  10. => ValidateType(type, paramName, false, false);
  11. // Token: 0x0600197D RID: 6525 RVA: 0x00053C07 File Offset: 0x00051E07
  12. public static void ValidateType(Type type, string paramName, bool allowByRef, bool allowPointer)
  13. {
  14. if (ValidateType(type, paramName, -1))
  15. {
  16. if (!allowByRef && type.IsByRef)
  17. {
  18. throw new ArgumentException("Type must not be ref", paramName);
  19. }
  20. if (!allowPointer && type.IsPointer)
  21. {
  22. throw new ArgumentException("Type must not be pointer", paramName);
  23. }
  24. }
  25. }
  26. // Token: 0x0600197E RID: 6526 RVA: 0x00053C37 File Offset: 0x00051E37
  27. public static bool ValidateType(Type type, string paramName, int index)
  28. {
  29. if (type == typeof(void))
  30. return false;
  31. if (type.ContainsGenericParameters)
  32. throw type.IsGenericTypeDefinition
  33. ? new ArgumentException($"Type {type} is a generic type definition", GetParamName(paramName, index))
  34. : new ArgumentException($"Type {type} contains generic parameters", GetParamName(paramName, index));
  35. return true;
  36. }
  37. public static string GetParamName(string paramName, int index)
  38. {
  39. if (index >= 0)
  40. {
  41. return string.Format("{0}[{1}]", paramName, index);
  42. }
  43. return paramName;
  44. }
  45. public static bool AreEquivalent(Type t1, Type t2)
  46. {
  47. return t1 != null && t1 == t2;
  48. }
  49. public static bool AreReferenceAssignable(Type dest, Type src)
  50. {
  51. return TypeUtils.AreEquivalent(dest, src) || (!dest.IsValueType && !src.IsValueType && dest.IsAssignableFrom(src));
  52. }
  53. }
  54. }