首页 > 解决方案 > C# 检查在命名方法中设置了哪些参数

问题描述

如果可能的话,我希望有人可以帮助我或给我一个信息......

我想检查一个命名方法,哪些参数真正设置并传递给它。可以制作字典并通过 KeyValue-Pair 将参数传递给方法,但是还有其他解决方案吗?

我可以检查堆栈跟踪中的当前方法并收集该方法的当前设置参数吗?

为了更好地理解,我创建了一个示例来可视化该问题:

class Program
{
    static void Main(string[] args)
    {
        TestClass newC = new TestClass("Init", 10);
        newC.ToString();    //{varStr: "Init"; varInt: 10}

        newC.update(vStr: "ok");

        newC.ToString();    //{varStr: "ok"; varInt: 0}     
                            // !!! but should have {varStr: "ok"; varInt: 10} !!!
        Console.WriteLine("<-- press any key to exit -->");
        Console.ReadKey();
    }
}

class TestClass
{
    string varStr;
    int varInt;

    public TestClass(string vStr, int vInt)
    {
        varStr = vStr;
        varInt = vInt;
    }

    public void update(string vStr = default, int vInt = default)
    {
        //here check it if vStr-Param was set and set varStr only if it was passed to it!
        //TODO
        varStr = vStr;
        //here check it if vInt-Param was set and set varInt only if it was passed to it!
        //TODO
        varInt = vInt;
    }

    public override string ToString()
    {
        Console.WriteLine($"TestClass: varStr: {varStr}; varInt: {varInt};");
        return null;
    }
}

有没有办法实现这一点,就像我想的那样?

编辑:

使用默认值并检查它!

也可以在这个方法上设置默认值!因此,检查默认值并且仅当它等于我们设置的值时,对我们来说是没有选择的解决方案。

我们还有一个“自定义类”,我们提供了该方法,它也可以为空。

使用重载方法!

问题是这个类没有 2 个变量,它有超过 15 个,并且编写每个重载方法将是一个很大的开销。

解决方案:

我找到了一个没有声明默认值的解决方案......所以每个值都可以设置!

    class TestClass
{
    string varStr;
    int varInt;
    TestClass1 varCustomClass;

    public TestClass(string vStr, int vInt, TestClass1 vCustomClass)
    {
        varStr = vStr;
        varInt = vInt;
        varCustomClass = vCustomClass;
    }

    public void update(Opt<int> varInt = default(Opt<int>),
                       Opt<string> varStr = default(Opt<string>),
                       Opt<TestClass1> varCustomClass = default(Opt<TestClass1>))
    {
        if (varStr.HasValue)
        {
            this.varStr = varStr.Value;
        }
        if (varInt.HasValue)
        {
            this.varInt = varInt.Value;
        }
        if (varCustomClass.HasValue)
        {
            this.varCustomClass = varCustomClass.Value;
        }
    }

    public override string ToString()
    {
        Console.WriteLine($"TestClass3: varStr: {varStr}; varInt: {varInt}; varCustomClass: {varCustomClass};");
        return null;
    }
}

public struct Opt<T>
{
    public Opt(T value)
    {
        _value = value;
        _hasValue = true;
    }

    public static explicit operator T(Opt<T> optional)
    {
        return optional._value;
    }

    public static implicit operator Opt<T>(T value)
    {
        return new Opt<T>(value);
    }

    T _value;
    public T Value
    {
        get { return _value; }
    }

    bool _hasValue;
    public bool HasValue
    {
        get { return _hasValue; }
    }
}

标签: c#methods

解决方案


您的字符串也可能为空吗?如果不是,您可以检查它是否为空。对于您的 int,您可以使用可为空的值类型:

public void update(string vStr = null, int? vInt = null)
    {
        if(vStr != null)
        {
             varStr = vStr;
        }
        if(vInt != null)
        {
            varInt = vInt.Value;
        }
    }

如果你只有几个参数并且它们都有不同的类型,重载方法也是一个很好的解决方案:

public void update(string vStr)
 {
 varStr = vStr;
 }

public void update(int vInt)
 {
 varInt = vInt;
 }

public void update(string vStr, int vInt)
 {
 update(vStr);
 update(vInt);
 }

推荐阅读