locked
Error 1 Operator '<' cannot be applied to operands of type 'method group' and 'System.Type'

    Question

  •  

    Hi All,

     

    I am using VS 2005 and working on unit testing. I want to do somewhat like this:

     

    Assembly oAssembly = Assembly.Load("MyAssembly"); //Assembly name is coming from database as fully qualified name

    Type oTypeFacade = oAssembly.GetType("MyAssembly.MyFacade"); //Facade class name in coming from database as fully qualified name

    IMyInterface oObject = Activator.CreateInstance(oTypeFacade, new object[] { oContext }) as IMyInterface ; //Interface is known, its method that facade has to be implementated

    MyCriteria oCriteria = GetCriteriaObject(oTypeFacade, testRow, oContext) as MyCriteria;

    Type oTypeFetch = oAssembly.GetType("MyAssembly.MyBusinessObject"); //BusinessObject class name is from database.

    object actual = oObject.Fetch < oTypeFetch> (oCriteria); // I am getting compilation here.

     

    Error 1 Operator '<' cannot be applied to operands of type 'method group' and 'System.Type'

     

    Do some one know how to tackle this issue? I do not want to write switch cases, as it will not remain generalized test method. 

     

    Thanks in advance,

    Pravin

    Monday, March 24, 2008 8:25 AM

Answers

  • oTypeFetch is not a type name, but an instance of the Type class. The compiler must know at compile-time which generic method to call.

    You can call the generic method through reflection, by changing the last line of your sample with:

    MethodInfo fetchMethod = oObject.GetType().GetMethod("Fetch", new Type[] { typeof(MyCriteria) });
    MethodInfo genericFetchMethod = fetchMethod.MakeGenericMethod(oTypeFetch);

    genericFetchMethod.Invoke(oObject, new object[] { oCriteria });

    Monday, March 24, 2008 9:37 AM

All replies

  • oTypeFetch is not a type name, but an instance of the Type class. The compiler must know at compile-time which generic method to call.

    You can call the generic method through reflection, by changing the last line of your sample with:

    MethodInfo fetchMethod = oObject.GetType().GetMethod("Fetch", new Type[] { typeof(MyCriteria) });
    MethodInfo genericFetchMethod = fetchMethod.MakeGenericMethod(oTypeFetch);

    genericFetchMethod.Invoke(oObject, new object[] { oCriteria });

    Monday, March 24, 2008 9:37 AM
  • Thanks you very much Philippe!!!

    It solved my problem and learnt new thing.

    Monday, March 24, 2008 10:05 AM