-1

I'd like to inject some other code to replace all usage of LINQ in all code files in my project, without having to change the individual files. Like this:

var arr = items.Where(i => i.Price > 10).Select(i => i.Name).ToArray();

Instead of invoking LINQ-extensions here I want to redirect to this class instead: Company.OtherLinqExtensions, globally in one place.

I have lots of "using System.Linq" here and there. How do I redirect it to Company.OtherLinqExtensions without changing all using statements?

Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
Andreas Zita
  • 7,232
  • 6
  • 54
  • 115
  • Can you provide an example of an expression that must be replaced? And how the method used from OtherLinqExtensions looks like? – Alexei - check Codidact Nov 12 '16 at 17:01
  • I found this question also which describes what I'm after in a better way perhaps: http://stackoverflow.com/questions/1451099/how-to-override-an-existing-extension-method – Andreas Zita Nov 13 '16 at 11:43

1 Answers1

0

Suppose you have some class in namespace My.Company, which uses LINQ as you described and has using System.Linq declaration. Then to "redirect" all calls to linq methods to your class, you just need to declare your class in the same My.Company namespace or in any parent namespace (like "My"):

namespace My.Company { // OR namespace My {
    public static class OtherLinqExtensions {
        public static IEnumerable<T> Select<T, TResult>(this IEnumerable<T> items, Func<T, TResult> selector) {
            // do your stuff
        }
    }
}

Then, all extensions method will now call OtherLinqExtensions extensions (provided of course that your versions exactly match standard linq declarations).

Evk
  • 98,527
  • 8
  • 141
  • 191