I want use a Linq IQueryable Toolkit in project on .NET Compact Framework. The Linq capabilities in CF is little bit shapred – i.e.: IQueryable interface is not available. So I’ve found third party libraries, which implements missing functionality what I need.
Now I have problem with missing method ‘MethodBase.GetCurrentMethod()’. There is cca 100 methods, which uses this method. So I don’t need the exact clone of ‘GetCurrentMethod()’. The workaround way for this specific case is enough.
Sample of original code:
public static bool Any<TSource>( this IQueryable<TSource> source ) { return source.Provider.Execute<bool>( Expression.Call( null, ((MethodInfo)MethodBase.GetCurrentMethod()).MakeGenericMethod( new Type[] { typeof( TSource ) } ), new Expression[] { source.Expression } ) ); } public static bool Any<TSource>( this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate ) { return source.Provider.Execute<bool>( Expression.Call( null, ((MethodInfo)MethodBase.GetCurrentMethod()).MakeGenericMethod( new Type[] { typeof( TSource ) } ), new Expression[] { source.Expression, Expression.Quote( predicate ) } ) ); }
The posibile solution is replace ‘(MethodInfo)MethodBase.GetCurrentMethod()’ with specific method call. For example: GetMethod_Any_TSource_On_Source() and GetMethod_Any_TSource_On_Source_With_Predicate_TSource_Bool().
I search for some handy solution how to solve it.
See this discussion
It is essentially impossible in plain managed code in Compact Framework 1.0.
In 2.0 is it possible but error prone, fragile and most importantly NOT guaranteed to be correct (a serious flaw).
I would suggest instead writing a macro which can find all instances of ‘((MethodInfo)MethodBase.GetCurrentMethod())’ and determinte the method in which they reside.
Simply converting every line like so
to throw new Exception((MethodInfo)MethodBase.GetCurrentMethod()).Name);
This then gives you a list of what you need to put in directly at each call site (probably backed up by a lazily created static field holding the MethodInfo for each one.
This is cumbersome but might work reasonably well as a one off action pre framework update though to be honest it might be just as quick to go through and do it by hand.