using System.Linq.Expressions;
namespace Umbraco.Cms.Core;
///
/// Represents a simple in a form which is suitable for using as a dictionary key
/// by exposing the return type, argument types and expression string form in a single concatenated string.
///
public struct LambdaExpressionCacheKey
{
///
/// The argument type names of the
///
public readonly HashSet ArgTypes;
public LambdaExpressionCacheKey(string returnType, string expression, params string[] argTypes)
{
ReturnType = returnType;
ExpressionAsString = expression;
ArgTypes = new HashSet(argTypes);
_toString = null;
}
public LambdaExpressionCacheKey(LambdaExpression obj)
{
ReturnType = obj.ReturnType.FullName;
ExpressionAsString = obj.ToString();
ArgTypes = new HashSet(obj.Parameters.Select(x => x.Type.FullName));
_toString = null;
}
///
/// The return type of the
///
public readonly string? ReturnType;
///
/// The original string representation of the
///
public readonly string ExpressionAsString;
private string? _toString;
///
/// Returns a that represents this instance.
///
///
/// A that represents this instance.
///
public override string ToString() => _toString ??= string.Concat(string.Join("|", ArgTypes), ",", ReturnType, ",", ExpressionAsString);
///
/// Determines whether the specified is equal to this instance.
///
/// The to compare with this instance.
///
/// true if the specified is equal to this instance; otherwise, false.
///
public override bool Equals(object? obj)
{
if (ReferenceEquals(obj, null))
{
return false;
}
var casted = (LambdaExpressionCacheKey)obj;
return casted.ToString() == ToString();
}
///
/// Returns a hash code for this instance.
///
///
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
///
public override int GetHashCode() => ToString().GetHashCode();
}