나는 당신이 형태로 호출하는 검사 방법의 인수에 사용하는 클래스가 : 인수가 속성의 이름을 가진 ArgumentNullException
는 null입니다캐시 컴파일 <Func<T>>
public void SomeMethod(string anArg)
{
Ensure.ArgumentNotNull(() => anArg);
}
경우를 던졌습니다. 이것은과 같이 수행됩니다 GetMemberName
내가 작성한 확장 방법이다
public static void ArgumentNotNull<T>(Expression<Func<T>> expression) where T : class
{
var value = expression.Compile()();
if (value == null)
{
throw new ArgumentNullException(expression.GetMemberName());
}
}
.
내가 겪고있는 문제는 컴파일 속도가 매우 느리기 때문에 결과를 캐시하고 싶지만 고유 한 캐시 키를 찾을 수없는 것 같습니다. 캐시 충돌을 방지 할만큼 충분하지만 캐시가 유효하지 않게 유일하지는 않습니다.
internal static class ExpressionCache<T>
{
private static readonly Dictionary<string, Func<T>> Cache = new Dictionary<string, Func<T>>();
public static Func<T> CachedCompile(Expression<Func<T>> targetSelector)
{
Func<T> cachedFunc;
var cacheKey = targetSelector + targetSelector.Body.ToString();
if (!Cache.TryGetValue(cacheKey, out cachedFunc))
{
cachedFunc = targetSelector.Compile();
Cache[cacheKey] = cachedFunc;
}
return cachedFunc;
}
}
그러나 이것은 여전히 캐시 키 충돌이 발생합니다
내 최선의 노력은 지금까지입니다. 더 나은 접근 방법은 무엇일까요?
저는 PostSharp 또는 IL을 사용합니다 : http://abdullin.com/journal/2008/12/19/how-to-get-parameter-name-and-argument-value-from-c-lambda- v.html –
@Ruben, 게시하기 전에 브라우저에서 약간의 해킹을 한 것 같습니다. 나는 그것을 바로 잡을 것이다. – ilivewithian
이유식이 필요한 이유를 물어봐도됩니다. 님이 인수 검사기에 입력 한 이유는 무엇입니까? 그 예외를 던질 수있을 뿐인가? –
asgerhallas