none
Signature for methods that leverage string indexing RRS feed

  • Question

  • I am working with some code that uses the WIX toolset Session object. Currently a method is passes an instance of the session object, however it only access the string indexer (through an extension method). This is awkward to test, and as an indexer is not represented like a collection for example, I can't shorten the scope of the extension methods signature to prevent passing the entire session object to the enclosing method. For example:
    static void WantToTest(Session session, string otherParams)
    {
      // ...
    
      session.ExtMethodThatOnlyUsesIndexer(foo, bar);
    
      // ...
    }
    Any insight would be appreciated.
    Friday, November 3, 2017 3:48 PM

Answers

  • Indexers aren't supported as part of generic constraints or whatnot if that is what you're after. What is the issue with simply passing the Session object?

    Looking at Session, there really isn't any way to generalize it. It defines the indexer directly and doesn't implement any sort of interface or base type from which you could narrow down the type. The best you could probably do is use a lambda expression (i.e. Func<string>). Then the caller could use the indexer directly. But I don't know how that would play with your extension method.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Ritmo2k Friday, November 3, 2017 6:08 PM
    Friday, November 3, 2017 5:43 PM
    Moderator

All replies

  • Indexers aren't supported as part of generic constraints or whatnot if that is what you're after. What is the issue with simply passing the Session object?

    Looking at Session, there really isn't any way to generalize it. It defines the indexer directly and doesn't implement any sort of interface or base type from which you could narrow down the type. The best you could probably do is use a lambda expression (i.e. Func<string>). Then the caller could use the indexer directly. But I don't know how that would play with your extension method.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Ritmo2k Friday, November 3, 2017 6:08 PM
    Friday, November 3, 2017 5:43 PM
    Moderator
  • I feared you were right regarding how indexers are represented. I have been looking at Moq4 and I am probably bike shedding on this, as you indicate there is probably nothing wrong with passing the session object as I can just mock all the functionality.

    Thanks for the confirmation.

    Friday, November 3, 2017 6:10 PM