Giter Site home page Giter Site logo

valuegetter's Introduction

English | δΈ­ζ–‡


NuGet


Online Demo

ValueGetter Demo | .NET Fiddle


Feature:

  • 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

Installation

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

GetStart:

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);

GetPropertiesFromCache

var data = new MyClass { MyProperty1 = 123, MyProperty2 = "123" };
var props = data.GetPropertiesDictionaryFromCache();  //IDictionary<string, PropertyInfo>
or
var props = data.GetPropertiesFromCache(); //IList<PropertyInfo>

BenchmarkDotNet

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

valuegetter's People

Contributors

shps951023 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

ck-k1vv9 voltura

valuegetter's Issues

Support ExpandoObject

Support ExpandoObject

        [TestMethod]
        public void ExpandoObjectTest()
        {
            dynamic data = new System.Dynamic.ExpandoObject();
            data.MyProperty1 = 123;
            data.MyProperty2 = "test";

            var result = data.GetObjectValues(); //Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: ''System.Dynamic.ExpandoObject' does not contain a definition for 'GetObjectValues''

            Assert.AreEqual(123, result["MyProperty1"]);
            Assert.AreEqual("test", result["MyProperty2"]);
        }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.