通过扩展方法进行调试

发布于 2024-12-02 10:14:35 字数 847 浏览 1 评论 0原文

我在 C# 中创建了一个方法来扩展字符串数据类型,为 Split 函数创建一个额外的重载,以便可以定义文本限定符。 示例 字符串数据定义为“字段 1”、“字段 2”、“字段 3”

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

扩展工作正常。一旦引用并使用命名空间,我就可以访问该方法。但是,我尝试调试的方法存在问题,但调试器不会进入扩展方法。

扩展代码

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

nUnit Test 中的代码

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

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

Assert.AreEqual(expectedCount, result.Length);

我无法单步执行 testString.Split(',','"')。它返回结果,并且智能感知会显示扩展方法。调试器只是跳过它,就像内置的 Split 方法一样

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??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半衾梦 2024-12-09 10:14:35

事实上,当您调用 testString.Split(',','"') 时,实际上调用的是 public string[] Split(params char[ ] 分隔符) 重载,而不是您的扩展方法。这是因为实例成员(如果适用)始终 优先于扩展方法。

您唯一可以做的两件事是重命名您的方法或以某种方式更改签名,使其与各种不同String.Split 重载。

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.

〆一缕阳光ご 2024-12-09 10:14:35

它应该像 StringExtension.Split(...); 那样调用或者尝试下面

在此处输入图像描述

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

enter image description here

花之痕靓丽 2024-12-09 10:14:35

您可以在扩展方法中放置断点;执行将在那里停止。

但必须有另一种方法,一种合适且可用的方法。
我还没找到。

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.

海未深 2024-12-09 10:14:35

可以通过按 F12(转到 def)或查看 Reflector 输出来验证要调用的代码。我问了一个关于如何在 VS2010 调试器中执行此操作的链接问题。

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.

胡渣熟男 2024-12-09 10:14:35

在让 VS 中断扩展方法时遇到问题后,我来到这里,我什至重新启动,以防这是调试器问题。

结果我在同名的不同方法中设置了断点。别像我一样。健康饮食并改善睡眠。

I landed here after having a problem getting VS to break in an extension method, I even restarted in case it was a debugger issue.

Turns out I had my breakpoint in a different method with the same name. Don't be like me. Eat healthy and fix your sleep.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文