forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinqBatchReflectHelper.cs
29 lines (25 loc) · 1.1 KB
/
LinqBatchReflectHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Linq.Expressions;
using System.Reflection;
using NHibernate.Util;
namespace NHibernate.Multi
{
static class LinqBatchReflectHelper
{
private static readonly ConcurrentDictionary<System.Type, Func<ILinqBatchItem, IList>> GetResultsForTypeDic = new ConcurrentDictionary<System.Type, Func<ILinqBatchItem, IList>>();
private static readonly MethodInfo GetTypedResultsMethod = ReflectHelper.GetMethod((ILinqBatchItem i) => i.GetTypedResults<object>()).GetGenericMethodDefinition();
internal static IList GetTypedResults(ILinqBatchItem batchItem, System.Type type)
{
return GetResultsForTypeDic.GetOrAdd(type, t => CompileDelegate(t)).Invoke(batchItem);
}
private static Func<ILinqBatchItem, IList> CompileDelegate(System.Type type)
{
var generic = GetTypedResultsMethod.MakeGenericMethod(type);
var instance = Expression.Parameter(typeof(ILinqBatchItem));
var methodCall = Expression.Call(instance, generic);
return Expression.Lambda<Func<ILinqBatchItem, IList>>(methodCall, instance).Compile();
}
}
}