【问题标题】:symbol argument for function input parameter函数输入参数的符号参数
【发布时间】:2014-03-09 17:40:41
【问题描述】:

我正在编写 c# 代码,我想在函数中传递一个参数,该参数将是这些符号之一 、==、!=

private Image ThresholdImageFilter(image, symbol, threshold)
{

for (int i = 1; i < 512; i++)
{
   for (int j = 1; j < 512; j++)
    {
       for (int j = 1; j < 512; j++)
         {
          if pixel symbol threshold;
               {
                     pixel = 1;
               }
          else
               {
                     pixel = 0;
               }
         }
    }
  }

}

我想多次调用一个函数,但使用不同的符号,像这样:

AImage = ThresholdImageFilter(image > threshold);
BImage = ThresholdImageFilter(image < threshold);
CImage = ThresholdImageFilter(image <= threshold);
DImage = ThresholdImageFilter(image >= threshold);

我正在寻找这样的东西:

private Image Filter(Bitmap image, symbolArgument symbol, int threshold)
{
     ...
}

最后像这样调用函数:

BinaryImage = Filter(imageB, >, threshold);

有办法吗?如果是这样,请建议如何传递 >= 或

【问题讨论】:

  • 你想在这里发生什么?这真的不清楚。 Filter 是否接受 bool 或其他?
  • 你想让你的函数接受Expression吗?
  • Filter 获取一张图片,将元素与阈值进行比较,然后输出另一张图片
  • 过滤器写好了

标签: c# arguments symbols


【解决方案1】:

让过滤器接受一个接受两个参数并返回布尔值的委托。然后,您可以使用给定的运算符将 lambda 传递给该参数:

public SomeResult Filter(Func<ArgumentType, ArgumentType, bool> predicate)
{
    if(predicate(operand1, operand2))
        DoSomething();
}

那么你可以这样称呼它:

var result = Filter((a,b)=> a > b);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多