-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TypeKey struct and fix IsArray type FullName
Introduced a new struct, TypeKey, to uniquely identify a Type and facilitate its usage in a dictionary as a key. Updated TypeExtensionMethods to make use of this new struct for its various caches, improving organization and access to these cached values. Added a new test method in FullNameTest.cs to verify cache functionality.
- Loading branch information
Showing
3 changed files
with
110 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
namespace X39.Util; | ||
|
||
/// <summary> | ||
/// Represents a key that uniquely identifies a Type. | ||
/// </summary> | ||
/// <remarks> | ||
/// This is useful for using a Type as a key in a dictionary. | ||
/// </remarks> | ||
public readonly struct TypeKey : IEquatable<TypeKey> | ||
{ | ||
/// <inheritdoc /> | ||
public bool Equals(TypeKey other) | ||
{ | ||
return Type.IsEquivalentTo(other.Type); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override bool Equals(object? obj) | ||
{ | ||
return obj is TypeKey other && Equals(other); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override int GetHashCode() | ||
{ | ||
return Type.GetHashCode(); | ||
} | ||
|
||
/// <summary> | ||
/// Determines whether two <see cref="TypeKey"/> objects are equal. | ||
/// </summary> | ||
/// <param name="left">The left <see cref="TypeKey"/> object.</param> | ||
/// <param name="right">The right <see cref="TypeKey"/> object.</param> | ||
/// <returns> | ||
/// <see langword="true"/> if the specified <see cref="TypeKey"/> objects are equal; otherwise, <see langword="false"/>. | ||
/// </returns> | ||
public static bool operator ==(TypeKey left, TypeKey right) | ||
{ | ||
return left.Equals(right); | ||
} | ||
|
||
/// <summary> | ||
/// Determines whether two <see cref="TypeKey"/> objects are not equal. | ||
/// </summary> | ||
/// <param name="left">The left <see cref="TypeKey"/> object.</param> | ||
/// <param name="right">The right <see cref="TypeKey"/> object.</param> | ||
/// <returns> | ||
/// <see langword="true"/> if the specified <see cref="TypeKey"/> objects are not equal; otherwise, <see langword="false"/>. | ||
/// </returns> | ||
public static bool operator !=(TypeKey left, TypeKey right) | ||
{ | ||
return !left.Equals(right); | ||
} | ||
|
||
/// <summary> | ||
/// Represents a key that uniquely identifies a Type. | ||
/// </summary> | ||
/// <remarks> | ||
/// This is useful for using a Type as a key in a dictionary. | ||
/// </remarks> | ||
public TypeKey(Type type) | ||
{ | ||
Type = type; | ||
} | ||
|
||
/// <summary> | ||
/// The Type that this TypeKey represents. | ||
/// </summary> | ||
public Type Type { get; } | ||
|
||
/// <summary> | ||
/// Defines an implicit operator that converts a TypeKey to a Type. | ||
/// </summary> | ||
/// <param name="key">The TypeKey to be converted.</param> | ||
/// <returns>The corresponding Type object.</returns> | ||
public static implicit operator Type(TypeKey key) => key.Type; | ||
|
||
/// <summary> | ||
/// Defines an implicit operator that converts a Type object to a TypeKey. | ||
/// </summary> | ||
/// <param name="type">The Type object to be converted.</param> | ||
/// <returns>The corresponding TypeKey object.</returns> | ||
public static implicit operator TypeKey(Type type) => new TypeKey(type); | ||
} |