Giter Site home page Giter Site logo

kwargs.net's Introduction

Kwargs.Net

NuGet
This library allows dotnet developers to use **kwargs (Keyword Arguments) to create instance or call function like python developers

Example

CreateInstance

using CreateInstance;
using Kwargs.Net;

Dictionary<string, object> kwargs = new()
{
    { "v1", 5 },
    { "s1", "123" },
};
TestClass testClass = Reflection.CreateInstance<TestClass>(Array.Empty<object>(), kwargs)!;
Console.WriteLine($"V1: {testClass.V1}, S1: {testClass.S1}, V2: {testClass.V2}, S2: {testClass.S2}, F1: {testClass.F1}");
// V1: 5, S1: 123, V2: , S2: , F1:

kwargs = new()
{
    { "v2", 7 },
};
testClass = Reflection.CreateInstance<TestClass>(new object[] { 5, "123" }, kwargs)!;
Console.WriteLine($"V1: {testClass.V1}, S1: {testClass.S1}, V2: {testClass.V2}, S2: {testClass.S2}, F1: {testClass.F1}");
// V1: 5, S1: 123, V2: 7, S2: , F1:

kwargs = new()
{
    { "f1", 5f },
    { "s1", "123" },
};
testClass = Reflection.CreateInstance<TestClass>(new object[] { 5 }, kwargs)!;
Console.WriteLine($"V1: {testClass.V1}, S1: {testClass.S1}, V2: {testClass.V2}, S2: {testClass.S2}, F1: {testClass.F1}");
// V1: 5, S1: 123, V2: , S2: , F1: 5
public class TestClass
{
    public int? V1 { get; set; }
    public string? S1 { get; set; }
    public int? V2 { get; set; }
    public string? S2 { get; set; }
    public float? F1 { get; set; }

    public TestClass(int v1, string s1, int v2 = 5, string? s2 = null)
    {
        V1 = v1;
        S1 = s1;
        V2 = v2;
        S2 = s2;
    }

    public TestClass(int v1, string s1)
    {
        V1 = v1;
        S1 = s1;
    }

    public TestClass(int v1, string s1, int? v2 = null, float? f1 = 17)
    {
        V1 = v1;
        S1 = s1;
        V2 = v2;
        F1 = f1;
    }
}

CallStaticFunction

using CallStaticFunction;
using Kwargs.Net;

var result = Reflection.CallFunction(typeof(TestClass), nameof(TestClass.Test), Array.Empty<object>(), new Dictionary<string, object>());
Console.WriteLine(result);
// 123
public class TestClass
{
    public static void Test() { Console.WriteLine("123"); }
    public int Test(int a) { Console.WriteLine(a); return a + 1; }
}

CallObjectFunction

using CallObjectFunction;
using Kwargs.Net;

TestClass testClass = new();
var result = Reflection.CallFunction(testClass, nameof(testClass.Test), Array.Empty<object>(), new Dictionary<string, object> { { "a", 5 } });
Console.WriteLine(result);
// 5
// 6
public class TestClass
{
    public static void Test() { Console.WriteLine("123"); }
    public int Test(int a) { Console.WriteLine(a); return a + 1; }
}

kwargs.net's People

Contributors

chengyen-tang avatar

Watchers

 avatar

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.