title: Is there a way to get all defined operators on any type?
body: <p>Quick disclaimer: I realize a similar question has already been asked here <a href="https://stackoverflow.com/questions/1412757/how-to-get-defined-operators-for-a-type-in-net">How to get defined operators for a type in .net</a>, but since this question does not answer the question asked here as this question expands upon the one mentioned above, an answer to this question would be appreciated.</p>
  <p>I would like to obtain a list of all operators that can de applied to a type. To simplify the code snippets, I will only print the operators in the console though.
  For example, <code>DateTime</code> defines a <code>+</code>, two <code>-</code> operators, and one operator for each inequality <code>&lt;</code>, <code>&lt;=</code>, etc. and the <code>==</code> operators. I can get a comprehensive list of all these operators by using the following code:</p>
  <pre class="lang-cs prettyprint-override"><code>foreach (var method in typeof(DateTime).GetMethods())
  {
      if (method.IsSpecialName)
      {
          Console.WriteLine(method.Name);
      }
  }
  </code></pre>
  <p>which prints:</p>
  <pre><code>get_UtcNow
  op_Addition
  op_Subtraction
  op_Subtraction
  op_Equality
  op_Inequality
  op_LessThan
  op_LessThanOrEqual
  op_GreaterThan
  op_GreaterThanOrEqual
  get_Date
  ...
  </code></pre>
  <p>where every method beginning with op_ is the corresponding operator.</p>
  <p>However when applying this code to <code>int</code> or any other primitive type, the list is either incomprehensive or even empty.
  When applied to <code>typeof(int)</code> for example, there are no methods with special name and as such no operators, which is obviously wrong.</p>
  <p>I <em>think</em> this is because <code>DateTime</code> uses operator overloading while <code>int</code> does not and as such does not define the methods 'op_Addition', etc. The <code>-</code> operator for example on <code>int</code> can be pretty much directly translated to the <code>sub</code> instruction in assembly.</p>
  <p>If I wanted to get all operators for <code>int</code> anyways now, I could obviously just define a list of all operators defined on <code>int</code> and any other type, but since there are quite a few types, for which you cannot get all operators using the above method, like <code>string</code>, <code>float</code>, <code>Int64</code>, etc. I don't think this is a good approach.
  Alternatively, you can try to convert them to <code>dynamic</code> and just apply an operator then, but since this will throw an exception, if the operator is not defined, and seems very unelegant to me, I would still like to try and find a better way of doing this.</p>
  <pre class="lang-cs prettyprint-override"><code>TryMultiply&lt;float, string&gt;(5.0f, &quot;abc&quot;, out var a);
  
  
  bool TryMultiply&lt;TA, TB&gt;(TA a, TB b, out object result)
  {
      try
      {
          dynamic dA = (dynamic)a;
          dynamic dB = (dynamic)b;
          result = dA * dB;
          return true;
      }
      catch (RuntimeBinderException e)
      {
          result = default;
          return false;
      }
  }
  </code></pre>
  <p>And even then, you only know, that the <code>*</code> operator is not defined for <code>float</code> and <code>string</code>. But you cannot make a statement about any other types, which renders this approach basically useless.</p>