I’m using MiscUtil Operators for a while without any big problems. But now i found something that really bothers me:
byte first = 13;
byte second = 29;
byte result = MiscUtil.Operator.Add(first, second);
The simple expected result of this equation should be result == 42 but unfortunately this throws an InvalidOperationException:
The binary operator Add is not defined for the types 'System.Byte' and 'System.Byte'.
By taking a closer look at this odd behavior you’ll find out that System.Byte really doesn’t implement these operators. Within C# these types will be implicitly converted to an Int32 and that does implement these operators.
So the question is now: Is there any chance to get MiscUtil to work with byte and sbyte?
Technically,
intetc also don’t implement these operators. They are not provided by “operators” in the normal sense (which would involve a static-call), but represented directly by theaddop-code. Ultimately, the failure in this case is actually coming from theExpressionAPI:To fix it, MiscUtil would have to special-case the
byte/sbyteversions; something like:However! It has been a long time since I knew my keys for Jon’s repo ;p
Oddly enough, though, it isn’t all that hard to implement the entire thing in raw IL… I actually stumbled into (on a USB-drive) a very old .NET 2.0 (i.e. before
Expression) version of “generic operators” that I wrote many many years ago, which might do the job. Or easier: just patch MiscUtil locally to handlebyte/sbyte.