1

I have the following Method (I exemplify what I need in the comments of the method):

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;
}

I have to know if one or more values of the bool parameters are true. Since they are four, imagine the combination of if I would have to do to check if only one is true, or if more than one, which are true.

So, how can I cycle the current value of the incoming Method parameters without using the parameter variable itself?

VMAtm
  • 27,943
  • 17
  • 79
  • 125
Dillinger
  • 341
  • 3
  • 16
  • 2
    It really depends on what you are trying to do. However, whenever you come across a scenario like this, it usually means you have to take a step back and rethink your design. A little bit more insight on the requirements might help. As you describe it, you can just do an OR (||) of all the values and you should get what you need but I have a feeling there is more to it than that. – JuanR Dec 23 '16 at 18:33
  • without knowing context....I would use bitarray or array of bools. and use linq to determine if only one is true or even how many is true. – M.kazem Akhgary Dec 23 '16 at 18:35
  • I updated the question and added my logic in the comments of the method. Essentially each `bool` represent a `WHERE` in SQL Select. If it is the first `true`, it's one query syntax. For the additional `true`, adds to original syntax but is another syntax (hence know if is 1st `bool`or more). – Dillinger Dec 23 '16 at 18:53
  • Are you only interested in the count of values that are true or do you need the name of the parameter as well? – JuanR Dec 23 '16 at 19:03

2 Answers2

0

If you only want to know how many are true, you can turn them into integers and add their values:

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

If you need the name of the parameters, then you can create an anonymous object and read its properties:

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);
        }
    }
}
JuanR
  • 7,405
  • 1
  • 19
  • 30
  • Am i wrong or you just answered my question? I guess `ProperyInfo` + `GetValue()` is what i am looking for. I'm testing it now. – Dillinger Dec 23 '16 at 19:59
  • Well it only determines if has value, either `true` or `false`, and i need to know which of them are `true`/`false`. – Dillinger Dec 23 '16 at 20:03
  • Read the code one more time. It does exactly what you want. PInfo will have the name of the property that has value true. – JuanR Dec 24 '16 at 22:14
0

You can try some LINQ extensions, I think composition of Where and Select may be a solution, with a string.Join method on top:

// 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)))
Community
  • 1
  • 1
VMAtm
  • 27,943
  • 17
  • 79
  • 125