Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 循环和访问方法';s参数值_C#_Parameters_.net 4.0_Getparameter_Methodbase - Fatal编程技术网

C# 循环和访问方法';s参数值

C# 循环和访问方法';s参数值,c#,parameters,.net-4.0,getparameter,methodbase,C#,Parameters,.net 4.0,Getparameter,Methodbase,我有以下方法(我在方法的评论中举例说明我需要什么): 公共静态字典Foo(bool-os、bool-rad、bool-aci、bool-outr、string-distrito=null) { 如果(os==false&&rad==false&&aci==false&&outr==false) { 返回新字典(); } var parameters=MethodBase.GetCurrentMethod().GetParameters(); foreach(参数中的ParameterInfo参数

我有以下方法(我在方法的评论中举例说明我需要什么):

公共静态字典Foo(bool-os、bool-rad、bool-aci、bool-outr、string-distrito=null)
{
如果(os==false&&rad==false&&aci==false&&outr==false)
{
返回新字典();
}
var parameters=MethodBase.GetCurrentMethod().GetParameters();
foreach(参数中的ParameterInfo参数)
{
//我希望parameter.Value存在,因为:
//if(parameter.Value==true){
//x++
//如果(x==1)字符串s=“true”+parameter.Name;
//如果(x>1)s+=“附加真值”+参数名称;
// }
//s+=“结束”;
}
返回null;
}
我必须知道
bool
参数的一个或多个值是否为真。因为它们是四个,想象一下如果我要检查是否只有一个是真的,或者如果有多个是真的,那么我必须做的
if
的组合


那么,在不使用参数变量本身的情况下,如何循环输入方法参数的当前值呢

如果您只想知道有多少是真的,您可以将它们转换为整数并添加它们的值:

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);
        }
    }
}
公共静态字典Foo(bool-os、bool-rad、bool-aci、bool-outr、string-distrito=null)
{
var obj=新的{os,rad,aci,outr};
foreach(obj.GetType().GetProperties()中的PropertyInfo pInfo)
{
var值=(bool)pInfo.GetValue(obj);
如果(值)
{
//在这里你想干什么就干什么。
WriteLine(“{0}:{1}”,pInfo.Name,value);
}
}
}

如果您只想知道有多少是真的,可以将它们转换为整数并添加它们的值:

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);
        }
    }
}
公共静态字典Foo(bool-os、bool-rad、bool-aci、bool-outr、string-distrito=null)
{
var obj=新的{os,rad,aci,outr};
foreach(obj.GetType().GetProperties()中的PropertyInfo pInfo)
{
var值=(bool)pInfo.GetValue(obj);
如果(值)
{
//在这里你想干什么就干什么。
WriteLine(“{0}:{1}”,pInfo.Name,value);
}
}
}
您可以尝试一些,我认为和的组合可能是一个解决方案,顶部有一个方法:

// 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)))
您可以尝试一些,我认为,和的组合可能是一个解决方案,顶部有一个方法:

// 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)))

这真的取决于你想做什么。然而,每当您遇到这样的场景时,通常意味着您必须后退一步,重新思考您的设计。对需求多了解一点可能会有所帮助。正如你所描述的,你可以对所有的值做一个OR(| |)运算,你应该得到你所需要的,但我觉得不止这些。在不知道上下文的情况下……我会使用位数组或布尔数组。并使用linq确定是否只有一个是真的,甚至有多少是真的。我更新了问题,并在方法的注释中添加了我的逻辑。基本上,每个
bool
表示SQL Select中的
WHERE
。如果它是第一个
true
,则它是一个查询语法。对于附加的
true
,添加到原始语法中,但却是另一种语法(因此知道是1st
bool
还是更多)。您是否只对true值的计数感兴趣,还是还需要参数的名称?这取决于您尝试执行的操作。然而,每当您遇到这样的场景时,通常意味着您必须后退一步,重新思考您的设计。对需求多了解一点可能会有所帮助。正如你所描述的,你可以对所有的值做一个OR(| |)运算,你应该得到你所需要的,但我觉得不止这些。在不知道上下文的情况下……我会使用位数组或布尔数组。并使用linq确定是否只有一个是真的,甚至有多少是真的。我更新了问题,并在方法的注释中添加了我的逻辑。基本上,每个
bool
表示SQL Select中的
WHERE
。如果它是第一个
true
,则它是一个查询语法。对于附加的
true
,添加到原始语法中,但这是另一种语法(因此知道是1st
bool
还是更多)。您只对true值的计数感兴趣,还是还需要参数的名称?我错了还是您刚刚回答了我的问题?我想
ProperyInfo
+
GetValue()
就是我要找的。我现在正在测试它。它只确定是否有值,或者是
true
或者
false
,我需要知道它们中哪些是
true
/
false
。请再读一遍代码。这正是你想要的。PInfo将具有值为true的属性的名称。是我错了还是您刚刚回答了我的问题?我想
ProperyInfo
+
GetValue()
就是我要找的。我现在正在测试它。它只确定是否有值,或者是
true
或者
false
,我需要知道它们中哪些是
true
/
false
。请再读一遍代码。这正是你想要的。PInfo将具有值为true的属性的名称。