【问题标题】:C# Check which Parameters are set in an Named-Method [duplicate]C#检查在命名方法中设置了哪些参数[重复]
【发布时间】:2021-02-25 06:15:45
【问题描述】:

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

我想检查一个命名方法,哪些参数真正设置并传递给它。 可以制作字典并通过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; }
    }
}

【问题讨论】:

  • if(vStr != default(string))?
  • 使用重载而不是可选参数。
  • if (vStr != default(string))。请注意string(和任何其他引用类型)的默认值是null,但int(和任何其他值类型)的默认值是实际值。 IE default(int) == 0 是真的
  • 只需检查您的变量是否具有默认值,如果没有分配新值。
  • 可能有重载方法

标签: c# methods


【解决方案1】:

你的字符串可能也为空吗?如果不是,您可以检查它是否为空。对于您的 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);
 }

【讨论】:

    猜你喜欢
    • 2018-01-22
    • 2014-06-07
    • 2021-09-15
    • 2010-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-20
    相关资源
    最近更新 更多