Files
Umbraco-CMS/src/Umbraco.Core/LambdaExpressionCacheKey.cs
shannon@ShandemVaio 62d6db935d Integrating v5 TypeFinder. Imported a few classes from v5 regarding utility classes and partial trust
unit test helper classes. Changed calls from old TypeFinder to new TypeFinder2. Created benchmark unit
tests to test speed differences between them.
Fixes: 30946 - IOHelper with HttpContext issue
Fixes: 30945 - AppCodeFileExtensions issue with UmbracoSettings
2012-07-26 21:12:54 +06:00

83 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
namespace Umbraco.Core
{
/// <summary>
/// Represents a simple <see cref="LambdaExpression"/> 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.
/// </summary>
internal struct LambdaExpressionCacheKey
{
public LambdaExpressionCacheKey(string returnType, string expression, params string[] argTypes)
{
ReturnType = returnType;
ExpressionAsString = expression;
ArgTypes = new HashSet<string>(argTypes);
_toString = null;
}
public LambdaExpressionCacheKey(LambdaExpression obj)
{
ReturnType = obj.ReturnType.FullName;
ExpressionAsString = obj.ToString();
ArgTypes = new HashSet<string>(obj.Parameters.Select(x => x.Type.FullName));
_toString = null;
}
/// <summary>
/// The argument type names of the <see cref="LambdaExpression"/>
/// </summary>
public readonly HashSet<string> ArgTypes;
/// <summary>
/// The return type of the <see cref="LambdaExpression"/>
/// </summary>
public readonly string ReturnType;
/// <summary>
/// The original string representation of the <see cref="LambdaExpression"/>
/// </summary>
public readonly string ExpressionAsString;
private string _toString;
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public override string ToString()
{
return _toString ?? (_toString = String.Concat(String.Join("|", ArgTypes), ",", ReturnType, ",", ExpressionAsString));
}
/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object obj)
{
if (ReferenceEquals(obj, null)) return false;
var casted = (LambdaExpressionCacheKey)obj;
return casted.ToString() == ToString();
}
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode()
{
return ToString().GetHashCode();
}
}
}