ValueGetter Demo | .NET Fiddle
- Mini (you can just copy/paste a ValueGetter.cs file to project)
- Faster Than Reflection GetValue (Click To [BenchmarkDotNet])
- Support
net40;net45;net451;net46;netstandard2.0;
frameworks
You can install the package from NuGet using the Visual Studio Package Manager or NuGet UI:
PM> install-package ValueGetter
or the dotnet
command line:
dotnet add package ValueGetter
Get all the property values of object, return Dictionary<string,object>
type
var data = new MyClass() { MyProperty1 = 123, MyProperty2 = "test" };
var result = data.GetObjectValues();
//Result:
Assert.AreEqual(123, result["MyProperty1"]);
Assert.AreEqual("test", result["MyProperty2"]);
Get all properties ToString value of the object, return Dictionary<string,string>
type
var data = new MyClass() { MyProperty1 = 123, MyProperty2 = "test" };
var result = data.GetToStringValues();
//Result:
Assert.AreEqual("123", result["MyProperty1"]);
Assert.AreEqual("test", result["MyProperty2"]);
Get single property value of object,return object
type.
var data = new MyClass() { MyProperty1 = 123 };
var prop = data.GetType().GetProperty("MyProperty1");
var result = prop.GetObjectValue(data);
//Result:
Assert.AreEqual(123, result);
Get single property ToString value of object,return string
type.
var data = new MyClass() { MyProperty1 = 123 };
var prop = data.GetType().GetProperty("MyProperty1");
var result = prop.GetToStringValue(data);
//Result:
Assert.AreEqual("123", result);
var data = new MyClass { MyProperty1 = 123, MyProperty2 = "123" };
var props = data.GetPropertiesDictionaryFromCache(); //IDictionary<string, PropertyInfo>
or
var props = data.GetPropertiesFromCache(); //IList<PropertyInfo>
Logic:
public class BenchmarkBase
{
private static List<MyClass> Data = Enumerable.Range(1,100).Select(s=>new MyClass() { MyProperty1 = 123, MyProperty2 = "test" }).ToList();
[Benchmark()]
public void Reflection() => Data.Select(instance => {
var type = instance.GetType();
var props = type.GetProperties();
return props.ToDictionary(key => key.Name, value => value.GetValue(instance));
}).ToList();
[Benchmark()]
public void ReflectionToString() => Data.Select(instance => {
var type = instance.GetType();
var props = type.GetProperties();
return props.ToDictionary(key => key.Name, value => value.GetValue(instance).ToString());
}).ToList();
[Benchmark()]
public void GetObjectValues() => Data.Select(s => s.GetObjectValues()).ToList();
[Benchmark()]
public void GetObjectToStringValues() => Data.Select(s => s.GetToStringValues()).ToList();
}
Result:
BenchmarkDotNet=v0.11.1, OS=Windows 10.0.17134.648 (1803/April2018Update/Redstone4)
Intel Core i7-7700 CPU 3.60GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores
[Host] : .NET Framework 4.7.2 (CLR 4.0.30319.42000), 64bit RyuJIT-v4.7.3362.0
ShortRun : .NET Framework 4.7.2 (CLR 4.0.30319.42000), 64bit RyuJIT-v4.7.3362.0
Method | Mean | Gen 0 | Allocated |
---|---|---|---|
GetObjectValues | 32.93 us | 9.8750 | 40.51 KB |
GetObjectToStringValues | 38.23 us | 10.0625 | 41.29 KB |
Reflection | 54.40 us | 10.0625 | 41.29 KB |
ReflectionToString | 60.24 us | 10.8125 | 44.42 KB |