1

Here is the Actual Implementation would look like without reflection :

IEnumerable<Foo> SelectWrapper(ExpressionNode orderClause)
{
    var data = context.Select<Foo>(new IClause<Foo>[]{new Clause<Foo, int> (orderClause)});
    return data;
}

But I have a generic method like below, I have been provided with unknown type t which will be Foo at runtime, so I tried like this :

IEnumerable<Type> SelectWrapper(Type t, ExpressionNode orderClause)
{
    MethodInfo getSelect = typeof(RepoClass).GetMethod("Select");
    MethodInfo genericSelect = getSelect.MakeGenericMethod(t);
    generic.Invoke(......); //how to invoke generic parameters here with generic method

    //....
    return someValue;
}

So my question is : Whats the possible way to implement this code via reflection ?

Many Thanks..

ThomasBecker
  • 388
  • 6
  • 20
  • I'd say compiled expression tree would be "better" than creating arguments one by one for every call... But since you have not shown code that constructs arguments it is unclear what part of code you would like to improve. (Also consider adding your criteria for "better"... note that "code that works correctly is *better* than code that does not compile" is *not* a good criteria). – Alexei Levenkov Apr 02 '14 at 16:57
  • I mean possible solution, Regarding expression tree, I have passed them in Clause class, but the problem is it can have multiple clauses, and with varying type. Therefore I had to do with reflection here, otherwise normal expression works fine. Thanks – ThomasBecker Apr 02 '14 at 17:07
  • 1
    You just need to create each object one by one... I'd recommend trying first - check out http://stackoverflow.com/questions/1151464/how-to-dynamically-create-generic-c-sharp-object-using-reflection/1151470#1151470 (which may be considered duplicate of your question, except it missing array creation one) – Alexei Levenkov Apr 02 '14 at 18:10

0 Answers0