【问题标题】:Cycle and access Method's parameters value循环和访问方法的参数值
【发布时间】:2017-05-09 10:32:36
【问题描述】:

我有以下方法(我举例我在方法的cmets中需要什么):

public static Dictionary<int, int> Foo(bool os, bool rad, bool aci, bool outr, string distrito = null)
{
    if (os == false && rad == false && aci == false && outr == false)
    {
        return new Dictionary<int, int>();
    }

    var parameters = MethodBase.GetCurrentMethod().GetParameters();
    foreach (ParameterInfo parameter in parameters)
    {
        // I would love if parameter.Value existed, because:
        // if (parameter.Value==true) {
        // x++
        // if (x == 1) string s = "true" + parameter.Name;
        // if (x > 1) s += "Additional true" + parameter.Name;
        // }
        // s += "End";
    }
    return null;
}

我必须知道bool 参数的一个或多个 值是否为真。由于它们是四个,想象一下if 的组合,我必须检查是否只有一个是真的,或者如果不止一个,这是真的。

那么,如何在不使用参数变量本身的情况下循环传入 Method 参数的当前值?

【问题讨论】:

  • 这真的取决于你想要做什么。然而,每当你遇到这样的场景时,这通常意味着你必须退后一步,重新考虑你的设计。对需求有更多的了解可能会有所帮助。正如您所描述的那样,您可以对所有值进行 OR (||),您应该得到您需要的东西,但我觉得还有更多的东西。
  • 不知道上下文....我会使用位数组或布尔数组。并使用 linq 来确定是否只有一个是真的,甚至有多少是真的。
  • 我更新了问题并在方法的 cmets 中添加了我的逻辑。基本上每个bool 代表SQL Select 中的WHERE。如果它是第一个true,它是一个查询语法。对于额外的true,添加到原始语法但是另一种语法(因此知道是否是第一个bool或更多)。
  • 您是只对真实值的计数感兴趣,还是还需要参数的名称?

标签: c# parameters .net-4.0 getparameter methodbase


【解决方案1】:

您可以尝试一些LINQ extensions,我认为WhereSelect 的组合可能是一个解决方案,顶部有string.Join 方法:

// get all parameters
var parameters = MethodBase.GetCurrentMethod().GetParameters();

// for each true parameter we add this prefix
var truePrefix = "true";

// we put this string between true parameters, if more than one are present
var separator = "Additional";

// Join IEnumerable of strings with a given separator
var total = string.Join(separator, parameters
  // filter parameters to check only boolean ones, and only true ones
  .Where(p => p.ParameterType == typeof(bool) && (bool)p.Value)
  // select a name with prefix
  .Select(p => truePrefix + p.Name)))

【讨论】:

    【解决方案2】:

    如果你只想知道有多少是真的,你可以把它们变成整数并加上它们的值:

    var values = new[] {os, rad, aci, outr};
    var sum = values.Select(v => Convert.ToInt32(v)).Sum();
    

    如果你需要参数的名称,那么你可以创建一个匿名对象并读取它的属性:

    public static Dictionary<int, int> Foo(bool os, bool rad, bool aci, bool outr, string distrito = null)
    {
        var obj = new { os, rad, aci, outr};
        foreach (PropertyInfo pInfo in obj.GetType().GetProperties())
        {
            var value = (bool)pInfo.GetValue(obj);
            if (value)
            {
                //Do whatever you want here.                    
                Console.WriteLine("{0}: {1}", pInfo.Name, value);
            }
        }
    }
    

    【讨论】:

    • 我错了还是你刚刚回答了我的问题?我想ProperyInfo + GetValue() 是我要找的。我现在正在测试它。
    • 好吧,它只确定是否有价值,truefalse,我需要知道它们中的哪一个是true/false
    • 再读一遍代码。它完全符合您的要求。 PInfo 将具有值为 true 的属性的名称。
    猜你喜欢
    • 1970-01-01
    • 2015-04-26
    • 2013-07-17
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 2011-12-21
    • 2012-10-05
    相关资源
    最近更新 更多