0

I am using Dynamic LINQ in an ASP.NET MVC project to create queries at runtime.

First I create a string from client's request like this:

string predicate = GetPredicateString(request);

The predicate would have a value like "AvailableFrom = DateTime(2015, 10, 05)"

Then in my code I call ParseLambda method of DynamicLinq.DynamicExpression class like below.

var lambda = DynamicExpression.ParseLambda(typeof(Product), typeof(bool), predicate)

Doing so I receive the following exception:

Unable to cast object of type 'System.Reflection.RuntimeConstructorInfo' to type 'System.Reflection.MethodInfo'.

Line 1958: int FindBestMethod(IEnumerable<MethodBase> methods, Expression[] args, out MethodBase method){
Line 1959: MethodData[] applicable =
               methods
                   .Select(m => new MethodData
                   { 
                       MethodBase = m,
                       Parameters = m.GetParameters()
                           .Where(p => p.ParameterType != ((MethodInfo)m).ReturnType).ToArray()
                   })
                   .Where(m => IsApplicable(m,args)).ToArray();

this part of the above fragment must be throwing the exception:

(MethodInfo)m

I have tried to pass arguments matching other constructor overloads of DateTime structure, but it doesn't make any difference. So one can rule out passing invalid arguments being the cause.

I have to say that I am getting this exception only with DateTime and all other data types (string, int etc.) cause no problem.

I haven't found anything similar in the Internet, so I guess either I am doing something wrong or this is a very rare situation.

Any idea on how to resolve this issue would be greatly appreciated.

Kamran
  • 782
  • 10
  • 35
  • 3
    A `ConstructorInfo` isn't a `MethodInfo`, so I'm not surprised that's failing. Why are you casting to `MethodInfo`? – Jon Skeet Sep 01 '15 at 13:00
  • @JonSkeet, this is the original code of Dynamic Linq library in `Dynamic.cs` file written by Microsoft not me! – Kamran Sep 01 '15 at 13:03
  • Right, well basically it appears not to support constructors... – Jon Skeet Sep 01 '15 at 13:04
  • But it is meant to work with `DateTime` and understands `"DateTime"` in a string as a keyword. – Kamran Sep 01 '15 at 13:07
  • 1
    Please copy and paste code, don't re-type it. You have introduced at least one syntax error that makes your question a lot harder to understand (missing `(`) by re-typing it. –  Sep 01 '15 at 13:08
  • @JonSkeet, a part of the "Dynamic Expressions and Quries in Link": Types The expression language defines the following primitive types: Object, Boolean, Char, String, SByte, Byte Int16, UInt16, Int32, UInt32, Int64, UInt64, Decimal, Single, Double, `DateTime`, TimeSpan, Guid – Kamran Sep 01 '15 at 13:08
  • `(MethodInfo)m)` is that a typo? – Amit Kumar Ghosh Sep 01 '15 at 13:10
  • @AmitKumarGhosh That's the one I was referring to. Looking up the actual source code online, yes, that is a typo, the actual source code has an extra `(`. –  Sep 01 '15 at 13:12
  • @AmitKumarGhosh, It is just a typo. The actual code is fine and builds without problem. – Kamran Sep 01 '15 at 13:13
  • @hvd, I copied that from inside `where` method and that the cause of the missing `(`. – Kamran Sep 01 '15 at 13:15
  • @Kamran It's wrong in your `Where`. What you put in the question is not the actual code. It can't be, because code with unbalanced parentheses won't compile. –  Sep 01 '15 at 13:16
  • wrong or not, he is casting when he doesn't need to. Just remove the `(MethodInfo)m` cast and just use `m`. – Philippe Paré Sep 01 '15 at 13:19
  • Typo fixed. But @PhilippeParé this is the original code of the Dynamic Library. I did not write it. – Kamran Sep 01 '15 at 13:21
  • 1
    @PhilippeParé That's what made it so confusing. It looked like it could just use `m` because of the missing `(`, but with the `(` added, it's clear why the cast is necessary: `MethodBase` doesn't have any `ReturnType` property. –  Sep 01 '15 at 13:36

0 Answers0