Ameisen

Untitled

May 31st, 2022
1,033
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.22 KB | None | 0 0
  1. [StructLayout(LayoutKind.Auto)]
  2. private readonly struct ReflectionCacheKey :
  3.         IEquatable<ReflectionCacheKey>
  4. {
  5.         private const bool Intern = false;
  6.  
  7.         internal readonly int Hash;
  8.         internal readonly Type MemberType { get; init; }
  9.         internal readonly bool IsStatic { get; init; }
  10.         internal readonly Type Type { get; init; }
  11.         internal readonly string MemberName { get; init; }
  12.  
  13.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
  14.         internal ReflectionCacheKey(Type memberType, bool isStatic, Type type, string memberName)
  15.         {
  16.                 if (ReflectionCacheKey.Intern)
  17.                 {
  18.                         memberName = string.Intern(memberName);
  19.                 }
  20.  
  21.                 this.MemberType = memberType;
  22.                 this.IsStatic = isStatic;
  23.                 this.Type = type;
  24.                 this.MemberName = memberName;
  25.  
  26.                 this.Hash = HashCode.Combine(
  27.                         memberType,
  28.                         isStatic,
  29.                         type,
  30.                         memberName
  31.                 );
  32.         }
  33.  
  34.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
  35.         readonly bool IEquatable<ReflectionCacheKey>.Equals(ReflectionCacheKey other) => this.Equals(in other);
  36.  
  37.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
  38.         internal readonly bool Equals(in ReflectionCacheKey other)
  39.         {
  40.                 return
  41.                         this.Hash == other.Hash &&
  42.                         this.MemberType == other.MemberType &&
  43.                         this.IsStatic == other.IsStatic &&
  44.                         this.Type == other.Type &&
  45.                         this.MemberName == other.MemberName;
  46.         }
  47.  
  48.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
  49.         public readonly override bool Equals(object? other)
  50.         {
  51.                 if (other is ReflectionCacheKey otherKey)
  52.                 {
  53.                         return this.Equals(otherKey);
  54.                 }
  55.  
  56.                 return false;
  57.         }
  58.  
  59.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
  60.         public static bool operator ==(in ReflectionCacheKey lhs, in ReflectionCacheKey rhs) => lhs.Equals(rhs);
  61.  
  62.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
  63.         public static bool operator !=(in ReflectionCacheKey lhs, in ReflectionCacheKey rhs) => !lhs.Equals(rhs);
  64.  
  65.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
  66.         public readonly override int GetHashCode() => this.Hash;
  67.  
  68.         [MethodImpl(MethodImplOptions.AggressiveInlining)]
  69.         public readonly override string ToString() =>
  70.                 $"{this.MemberType}{(this.IsStatic ? 's' : 'i')}{this.Type.FullName}:{this.MemberName}";
  71. }
Advertisement
Add Comment
Please, Sign In to add comment