title: How to call type conversion operator in C# generic class?
body: <p>It seems we cannot call type conversion operator <strong>easily</strong> in C# generic class. Here is the code. Why?</p>
  
  <p>T006 finally archive our target.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Diagnostics;
  using System.Reflection;
  using System.Linq;
  
  namespace ConsoleApplication1
  {
      class vec&lt;T, T2&gt; : List&lt;T&gt; where T : class
      {
          public vec(IEnumerable&lt;T2&gt; other)
          {
              //Converter&lt;T2, T&gt; cvt = (v) =&gt; (T)v; // T004 failed, try defined function dynamicly, cannot compile, too.
  
              // T006 pass, client happy, we not happy, but anyway passed, performance may not happy.
              var conversionOperator = typeof(T).GetMethods(BindingFlags.Static | BindingFlags.Public)
                                      .Where(m =&gt; m.Name == "op_Explicit" || m.Name == "op_Implicit")
                                      .Where(m =&gt; m.ReturnType == typeof(T))
                                      .Where(m =&gt; m.GetParameters().Length == 1 &amp;&amp; m.GetParameters()[0].ParameterType == typeof(T2))
                                      .FirstOrDefault();
              Func&lt;T2, T&gt; cvt = (obj) =&gt;
              {
                  if (conversionOperator != null)
                      return (T)conversionOperator.Invoke(null, new object[] { obj });
                  else
                      return default(T);
              };
  
              foreach (T2 item in other)
              {
                  //Add((T)item);         // T001 failed, this line cannot compile
                  //Add(item as T);       // T002 failed, this line alwasy return null. //  http://msdn.microsoft.com/en-us/library/vstudio/cscsdfbt.aspx
                  //Add((T)(object)item); // T003 failed, pass compile, but throw exception at runtime.
                  Add(cvt(item));         // T006 pass.
              }
          }
  
          // T005 pass, but clients for this code will not happy.
          public vec(Converter&lt;T2, T&gt; cvt, IEnumerable&lt;T2&gt; other)
          {
              foreach (T2 item in other)
              {
                  Add(cvt(item));
              }
  
          }
      }
  
      class XXX
      {
          public int foo = 22;
          static public explicit operator XXX(YYY other)
          {
              XXX me = new XXX();
              me.foo = (int)other.foo;
              return me;
          }
      }
  
      class YYY
      {
          public float foo = 11;
      }
  
  
  
      class Program
      {
          static void Main(string[] args)
          {
              YYY[] from = new YYY[2];
              for (int i = 0; i &lt; from.Length; i++)
              {
                  from[i] = new YYY();
              }
  
              XXX x = (XXX)from[0];
  
              vec&lt;XXX, YYY&gt; coll = new vec&lt;XXX, YYY&gt;(from);
  
              // Not happy, this requires user have strong C# skill;
              //vec&lt;XXX, YYY&gt; coll = new vec&lt;XXX, YYY&gt;((v) =&gt; (XXX)v, from);
  
              foreach (var item in coll)
              {
                  Debug.Print("Value is {0}", item.foo);
              }
  
          }
      }
  
  }
  </code></pre>
  
  <p>The compiler error for T001 is: <code>Cannot convert type 'T2' to 'T'</code></p>