简体   繁体   中英

How Can Return Dynamic methods with various parameters in C#

is it possible return dynamic methods with various parameters with Func or any methods like this

    public enum MethodType
    {
        T1,
        T2
    }
    Func<????, IRequest> MethodGenerator(MethodType methodType, Dictionary<Type, string> args)
    {
        switch (methodType)
        {
            case MethodType.T1:
                return Method1;
            case MethodType.T2:
                return Method2;
        }
        return null;
    }

    public IRequest Method1(string token, int state)
    {
        // Some Code
        return null;
    }
    public IRequest Method2(string token, int state, string name)
    {
        // Some Code
        return null;
    }
MethodGenerator(new Dictonary<type, string>(){MethodType.T1, {string, "token"},{int, "state"}}

and finally the last line return Method1 ?

There is no dynamic way to represent the definition of inputs for a Func but rather the type you define can be dynamic.

Given the following objects:

public enum MethodType
{
    T1,
    T2
}

public class TestRequest
{
    public IRequest Method1(string token, int state)
    {
        // Some Code
        return new Request1();
    }
    public IRequest Method2(string token, int state, string name)
    {
        // Some Code
        return new Request2();
    }
}

public interface IRequest
{
    string RequestName();
}

public class Request1 : IRequest
{
    public string RequestName() => "Request1";
}

public class Request2 : IRequest
{
    public string RequestName() => "Request2";
}

You can then have the following class:

public class TestClass
{
    private readonly Dictionary<MethodType, Func<object[], IRequest>> _definitions;

    public TestClass(Dictionary<MethodType, Func<object[], IRequest>> definitions)
    {
        _definitions = definitions;
    }

    public IRequest MethodGenerator(MethodType methodType,
        params object[] args)
    {
        return _definitions[methodType](args);
    }
}

I have written the following test to demonstrate the functionality:

    [TestMethod]
    public void TestMethod1()
    {
        var definitions = new Dictionary<MethodType, Func<object[], IRequest>>
        {
            {
                MethodType.T1, objects =>
                {
                    var testRequest = new TestRequest();
                    return testRequest.Method1((string)objects[0], (int)objects[1]);
                }
            },
            {
                MethodType.T2, objects =>
                {
                    var testRequest = new TestRequest();
                    return testRequest.Method2((string)objects[0], (int)objects[1], (string)objects[2]);
                }
            }
        };

        var testClass = new TestClass(definitions);
        var result1 = testClass.MethodGenerator(MethodType.T1, "test", 1);
        var result2 = testClass.MethodGenerator(MethodType.T2, "test2", 1, "test2");

        Assert.AreEqual("Request1", result1.RequestName());
        Assert.AreEqual("Request2", result2.RequestName());
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM