1

I'm currently working with generic classes where I want to find all the overloaded operators of the underlying generic type.
Therefor I use simple reflection like typeof(T).GetMethods(bindingFlags) and then filtering by methodInfo.isSpecialName and methodInfo.Name.StartsWith("op_"); While dealing with primitive types like int and float I got really surprised:
For float I realized there are the rational operators (op_Equality, op_Inequality, op_Greater etc..). But there are simply no arithmetic operators like op_Addition, op_Subtraction etc.
When I tried the same on int, I couldn't even find ration operators. And still no arithmetic ones in sight.
Is there any way to find those operators via reflection so I can treat those types like any other in my generic class? I simply want to generate a popup to choose from, including all operators (either rational or arithmetic).

The BindingFlags I combined using the |-operator:

Instance, Static, Public, NonPublic, FlattenHierarchy

Thanks for any help in advance, greetings BOTHLine

BOTHLine
  • 41
  • 6
  • I don't think it is possible - the C# built-in operators are implemented by the compiler and are not methods on the types. – NetMage Dec 20 '19 at 23:16
  • Yeah I thought that might be the case. I should be able to check for primitive types first and then use their special implementations. But then again I wonder.. why do I find the rational operators in a type like float, but not in int? I can't find anything that would explain this behavior in either System.Single or System.Int32 documentations. Both inherit from IComparable, which probably adds the rational operator functionality? – BOTHLine Dec 20 '19 at 23:54
  • `IComparable` just requires one method, `CompareTo`. Both `float` and `int` have the same interfaces. I think it is more a matter of inconsistency in .Net. It may have something to do with VB. – NetMage Dec 21 '19 at 00:32
  • Mb here? https://stackoverflow.com/questions/11113259/how-to-call-custom-operator-with-reflection – WhiteBlackGoose Jul 18 '20 at 11:30

0 Answers0