Skip to content

Commit 4e5e6f2

Browse files
author
Oren (electricessence)
committed
Implemented Expression untyped setter istead of PropertyInfo.SetValue.
1 parent c8886cc commit 4e5e6f2

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

Extensions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Data;
44
using System.Data.Common;
55
using System.Linq;
6+
using System.Linq.Expressions;
7+
using System.Reflection;
68
using System.Threading;
79
using System.Threading.Tasks;
810
using System.Threading.Tasks.Dataflow;
@@ -24,6 +26,20 @@ internal static bool IsStillAlive<T>(this ITargetBlock<T> task)
2426
return IsStillAlive(task.Completion);
2527
}
2628

29+
// https://stackoverflow.com/questions/17660097/is-it-possible-to-speed-this-method-up/17669142#17669142
30+
internal static Action<T, object> BuildUntypedSetter<T>(this PropertyInfo propertyInfo)
31+
{
32+
var targetType = propertyInfo.DeclaringType;
33+
var methodInfo = propertyInfo.GetSetMethod();
34+
var exTarget = Expression.Parameter(targetType, "t");
35+
var exValue = Expression.Parameter(typeof(object), "p");
36+
var exBody = Expression.Call(exTarget, methodInfo,
37+
Expression.Convert(exValue, propertyInfo.PropertyType));
38+
var lambda = Expression.Lambda<Action<T, object>>(exBody, exTarget, exValue);
39+
var action = lambda.Compile();
40+
return action;
41+
}
42+
2743
internal static object DBNullValueToNull(object value)
2844
=> value == DBNull.Value ? null : value;
2945

Open.Database.Extensions.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
<Description>Useful set of utilities and abstractions for simplifying modern database operations and ensuring DI compatibility.</Description>
1212
<RepositoryUrl>https://github.com/electricessence/Open.Database.Extensions</RepositoryUrl>
1313
<RepositoryType>git</RepositoryType>
14-
<Version>5.5.0</Version>
15-
<AssemblyVersion>5.5.0.0</AssemblyVersion>
16-
<FileVersion>5.5.0.0</FileVersion>
17-
<PackageReleaseNotes>Added ExecuteReturn and ExecuteReturnAsync for acquiring a single value from a return parameter.</PackageReleaseNotes>
14+
<Version>5.5.1</Version>
15+
<AssemblyVersion>5.5.1.0</AssemblyVersion>
16+
<FileVersion>5.5.1.0</FileVersion>
17+
<PackageReleaseNotes>Implemented Expression untyped setter istead of PropertyInfo.SetValue.</PackageReleaseNotes>
1818
</PropertyGroup>
1919

2020
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">

Transformer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public Processor(Transformer<T> transformer, string[] names = null)
5959
var name = _names[i];
6060
var value = record[i];
6161
if (value == DBNull.Value) value = null;
62-
p.SetValue(model, value);
62+
p(model, value);
6363
}
6464
}
6565

@@ -72,7 +72,7 @@ public Processor(Transformer<T> transformer, string[] names = null)
7272
public readonly Transformer<T> Transformer;
7373

7474
string[] _names;
75-
PropertyInfo[] _properties;
75+
Action<T, object>[] _properties;
7676

7777
public readonly Func<object[], T> Transform; // Using a Func<object[],T> for better type inferrence.
7878

@@ -81,7 +81,7 @@ public void SetNames(string[] names)
8181
var map = Transformer.ColumnToPropertyMap;
8282
_names = names;
8383
_properties = names
84-
.Select(n => map.TryGetValue(n.ToLowerInvariant(), out PropertyInfo p) ? p : null)
84+
.Select(n => map.TryGetValue(n.ToLowerInvariant(), out PropertyInfo p) ? p.BuildUntypedSetter<T>() : null)
8585
.ToArray();
8686
}
8787

0 commit comments

Comments
 (0)