4

iv'e been using the following reflection methods in order to create a Generic MethodInfo object of a certain type

// 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<t> 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 => methodInfo.Name == method_name && methodInfo.IsGenericMethodDefinition)
              .MakeGenericMethod(type);
     }
     public Iindexable GenerateIndex<T>()
     {
         return new Sindex<T>();    
     }
 } // end SomeClass  

now since these are methods that i use Frequently i decided to encapsulate them in a factory class

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<T>(string[] columns) 
       {// SIndex : IIndexable
            SIndex<T> index = new SIndex<T>(record, column, seecondary_columns);
            return index;
       }
       public Type GetType(string column)
}

now when i try to invoke the same method from somewhere outside the factory class i get a TargetException witch states, Object does not match target type.

// 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);
}

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 .

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

thanks in advance eran.

Jalal Said
  • 15,906
  • 7
  • 45
  • 68
eran otzap
  • 12,293
  • 20
  • 84
  • 139
  • I am very unclear on the sample; is there any chance you can re-phrase with a *reproducible* example, i.e. where I can run it and see the exception you are seeing? – Marc Gravell Jul 29 '11 at 09:10
  • the methods i didn't feel in are redundant ill edit the question to show the all picture if you think it'll help – eran otzap Jul 29 '11 at 09:23
  • i found the answer http://stackoverflow.com/questions/3860695/csystem-reflection-methodinfo-cause-object-does-not-match-target-type the target as to be the class that holds the generic method thous by sending it method.Invoke(factory,new object[]{columns}); except of method.Invoke(this,new object[]{columns}); i got it to work , i don't yet understand the target issue , i thought it was where it needs to return to after the methods been invoked .. any one know the reason for the target ? – eran otzap Jul 29 '11 at 10:12
  • the target is not where the it needs to return to after the method has been invoked.It is actually the object on which to invoke the method.since u are passing `this` from another class which doesnot have `GenerateIndex()` defined it is giving the error as the target type is your factory class and the class from where you are calling(`this`) doesn't match with that – Ashley John Jul 29 '11 at 11:25

1 Answers1

3

Try using

index_genrator.Invoke(f,null);
Strillo
  • 2,952
  • 13
  • 15