title: C# Generic Methods Problem ,TargetException , Object does not match target type
body: <p>iv'e been using the following reflection methods in order to create a Generic MethodInfo object of a certain type </p>
  
  <pre><code>// this is the example that does work , i call the invoked generic method 
  // from the same class it resides in 
  // all i'm doing here is casting type on T in order to create an object&lt;t&gt; of
  // some sort witch i can't know the T till run-time
  class SomeClass
  {
      public IIndexable CreateIndex(string column)
      {
         Type type = GetType(column);
         MethodInfo index_generator = GetGenericMethod(type,"GenerateIndex");
         Iindexable index = (Iindaxable)index_genraotr.Invoke(this,null); 
      }
  
  
       public MethodInfo GetGenericMethod(Type type, string method_name)
       {
          return GetType()
                .GetMethods(BindingFlags.Public |  BindingFlags.InstanceBindingFlags.NonPublic)
                .Single(methodInfo =&gt; methodInfo.Name == method_name &amp;&amp; methodInfo.IsGenericMethodDefinition)
                .MakeGenericMethod(type);
       }
       public Iindexable GenerateIndex&lt;T&gt;()
       {
           return new Sindex&lt;T&gt;();    
       }
   } // end SomeClass  
  </code></pre>
  
  <p>now since these are methods that i use Frequently i decided to encapsulate them in a factory class </p>
  
  <pre><code>class Factory
  {// same deal as above just that now i got a class called factory encapsulating 
   // all the functionality involved   
         public MethodInfo GetGenericMethod(Type type, string method_name)
  
         public IIndexable GenerateIndex&lt;T&gt;(string[] columns) 
         {// SIndex : IIndexable
              SIndex&lt;T&gt; index = new SIndex&lt;T&gt;(record, column, seecondary_columns);
              return index;
         }
         public Type GetType(string column)
  }
  </code></pre>
  
  <p>now when i try to invoke the same method from somewhere outside the factory class i get a <strong>TargetException</strong> witch states, 
  <strong>Object does not match target type.</strong> </p>
  
  <pre><code>// some event , or some place to call the factory's functionality from 
  btn_index ClickEvent(.......)
  {              
      Factory f = new Factory();
      Type type = f.GetType(column);
      MethodInfo index_generator = f.GetGenericMethod(type,"GenerateIndex");
      Iindexable index = (Iindexable)index_genrator.Inovke(this,null);
  }
  </code></pre>
  
  <p>is the target type the type of the place witch i call the invoke from ?
  and if so why should it matter the Method is found in the factory 
  when i call GetType() inside GetGenericMethod i get the type of the factory 
  and from there i extract the wanted method using a lambda expression .</p>
  
  <p>i would really appreciate if some one could shed some light on the matter  since i seem to be missing some know how on the matter </p>
  
  <p>thanks in advance eran.</p>