简体   繁体   中英

Debugging through an extension method

I've created a method in C# that extends the string datatype, creating an additional overload to the Split function so that a text qualifier can be defined. Example string data is defined as "field 1","field 2","filed 3"

string[] splitData = data.Split(',','"')

The extension works fine. I can access the method once I reference and use the namespace. However there is an issue in the method I'm trying to debug, but the debugger won't step into the extension method.

Extension Code

namespace Extensions
{
  public static class StringExtension
  {
    public static string[] Split(this string s, char delimiter, char qualifier)
    {
      // Method does work
    }
  }
}

Code in nUnit Test

string testString = "\"Field 1\",\"Field 2\",\"Field 3\"";
int expectedCount = 3;

// Do Test.
string[] result = testString.Split(',','"');

Assert.AreEqual(expectedCount, result.Length);

I can't step into testString.Split(',','"'). It returns a result and intellisense shows the extension method. The debugger just steps over it, as it would for the built in Split method.

Any ideas??

In fact, when you invoke testString.Split(',','"') what actually gets called is a public string[] Split(params char[] separator) overload, not your extension method. This is because instance members, if applicable, always take precedence over extension methods.

The only two things you can do are either rename your method or change signature somehow so it's different from various String.Split overloads.

It should call like StringExtension.Split(...); Or try below

在此输入图像描述

You can put a break point in the extension method; execution will stop there.

There must be another way though, one that is proper and usable.
I haven't found it yet.

One can verify code to be called by pressing F12 (go to def) or by looking at Reflector output. I have asked a linked question about how to do this in VS2010 debugger.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM