DynamicPropertyAccess 1.0.0
See the version list below for details.
dotnet add package DynamicPropertyAccess --version 1.0.0
NuGet\Install-Package DynamicPropertyAccess -Version 1.0.0
<PackageReference Include="DynamicPropertyAccess" Version="1.0.0" />
paket add DynamicPropertyAccess --version 1.0.0
#r "nuget: DynamicPropertyAccess, 1.0.0"
// Install DynamicPropertyAccess as a Cake Addin #addin nuget:?package=DynamicPropertyAccess&version=1.0.0 // Install DynamicPropertyAccess as a Cake Tool #tool nuget:?package=DynamicPropertyAccess&version=1.0.0
This library allows you to get or set values to properties by name. It uses compiled lambdas instead that are cached of reflection and is thread safe.
Getting Property Value
You can get property value by calling:
T ObjectExtensions.GetPropertyValue<T>(this object source, string propertyName)
Alternatively, you can create Func
that gets property value from an object by calling:
Func<object, object?> TypeExtensions.GetPropertyGetter(this Type type, string propertyName)
Setting Property Value
You can set property value by calling:
void ObjectExtensions.SetPropertyValue(this object source, string propertyName, object value)
Alternatively, you can create Func
that sets property value on an object by calling:
Action<object, object?> TypeExtensions.GetPropertySetter(this Type type, string propertyName)
Both methods have TryGet
pattern alternatives.
Benchmarks
You can see benchmark for GetPropertyValue
method for this library, DynamicPropertyAccessor, FastMember and Reflection.
Methods with ...Reused
suffix create Func
/PropertyInfo
once and then reuse it in benchmark.
BenchmarkDotNet=v0.13.5, OS=Windows 10 (10.0.19044.2604/21H2/November2021Update)
AMD Ryzen 7 3700X, 1 CPU, 16 logical and 8 physical cores
[Host] : .NET Framework 4.8 (4.8.4614.0), X64 RyuJIT VectorSize=256
.NET 7.0 : .NET 7.0.3 (7.0.323.6910), X64 RyuJIT AVX2
.NET Framework 4.7.2 : .NET Framework 4.8 (4.8.4614.0), X64 RyuJIT VectorSize=256
Method | Job | Runtime | Mean | Error | StdDev |
---|---|---|---|---|---|
DynamicPropertyAccess | .NET 7.0 | .NET 7.0 | 38.063 ns | 0.7854 ns | 1.0484 ns |
DynamicPropertyAccessor | .NET 7.0 | .NET 7.0 | 31.728 ns | 0.4040 ns | 0.3779 ns |
FastMember | .NET 7.0 | .NET 7.0 | 30.952 ns | 0.3137 ns | 0.2934 ns |
Reflection | .NET 7.0 | .NET 7.0 | 33.789 ns | 0.3169 ns | 0.2964 ns |
DynamicPropertyAccessReused | .NET 7.0 | .NET 7.0 | 1.677 ns | 0.0222 ns | 0.0208 ns |
FastMemberReused | .NET 7.0 | .NET 7.0 | 15.371 ns | 0.1688 ns | 0.1579 ns |
ReflectionReused | .NET 7.0 | .NET 7.0 | 12.011 ns | 0.1298 ns | 0.1215 ns |
DynamicPropertyAccess | .NET Framework 4.7.2 | .NET Framework 4.7.2 | 88.398 ns | 0.6464 ns | 0.5397 ns |
DynamicPropertyAccessor | .NET Framework 4.7.2 | .NET Framework 4.7.2 | 56.928 ns | 1.1533 ns | 1.0788 ns |
FastMember | .NET Framework 4.7.2 | .NET Framework 4.7.2 | 48.617 ns | 0.9610 ns | 1.0282 ns |
Reflection | .NET Framework 4.7.2 | .NET Framework 4.7.2 | 176.842 ns | 3.3967 ns | 4.0436 ns |
DynamicPropertyAccessReused | .NET Framework 4.7.2 | .NET Framework 4.7.2 | 6.066 ns | 0.0731 ns | 0.0684 ns |
FastMemberReused | .NET Framework 4.7.2 | .NET Framework 4.7.2 | 29.359 ns | 0.3300 ns | 0.2926 ns |
ReflectionReused | .NET Framework 4.7.2 | .NET Framework 4.7.2 | 121.696 ns | 2.4447 ns | 2.7173 ns |
As you can see, GetPropertyValue
is slightly slower than available alternatives.
This is because of thread safety overhead. On the other hand, reused Func
from GetPropertyGetter
is significantly faster.
Additionally, Reflection is almost as fast on .NET 7+.
You can read more about it here.
Conclusions
- Use this library (and reuse Getter/Setter) if:
- Read/Write operations are frequently performed.
- Thread safety is important.
- Use other libraries if:
- Read/Write operations are rarely performed.
- Thread safety is not a concern.
- Target framework is .NET Framework, .NET 6 or below.
- Use Reflection if:
- Target framework is .NET 7+ and Read/Write operations are rarely performed.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.