You need to understand that an operator can be considered as
syntactic sugar over the concept of
method. For example, you are talking about binary operators; functionally, they are equivalent to a function accepting two arguments (each representing an
operant) and returning the value. In your case, they return Boolean. Therefore, your arguments should be
delegate instances. Say, for
int
operands, the delegate type is
delegate bool Comparison(int left, int right)
.
But you don't need to do even that. You already have such declaration as generic type
System.Func<int, int, bool>
. Moreover, you can make your own class generic and use this generic delegate; this way, you can abstract from the types of your operands to be compared. First, let's consider the method of your non-generic class. It could be something like
using System;
public void ChangeColor(
Func<int, int, bool> lessThen,
Func<int, int, bool> greaterOrEqualThen ) {
if (lessThen != null) {
if (lessThen(myPixel, 0))
DoDomething();
else
DoSomethingElse();
} else
DoSomethingByDefault();
}
ChangeColor(
new Func<int, int, bool>((a, b) => { return a < b; }),
new Func<int, int, bool>((a, b) => { return a >= b; }));
I understand that this syntax of call is not as simple as you wanted, but this is what you have.
To to it in generic way, you can have something like
using System;
class GraphicProcessing<OPERAND> {
public void ChangeColor(
Func<OPERAND, OPERAND, bool> lessThen,
Func<OPERAND, OPERAND, bool> greaterOrEqualThen ) {
OPERAND myPixel =
OPERAND compareTo =
if (lessThen != null) {
if (lessThen(myPixel, compareTo))
DoDomething();
else
DoSomethingElse();
} else
DoSomethingByDefault();
}
}
Also note that you are trying to use very slow API,
GetPixel
, prohibitively slow. If you mean to use
System.Drawing
, such things can be done efficiently using
System.Drawing.Bitmap.LockBits
:
Bitmap.LockBits Method (System.Drawing)[
^].
You will find a very informative code sample on the MDSN help page on one of the
LockBits
methods referenced above.
—SA