通过扩展方法进行调试 [英] Debugging through an extension method

查看:91
本文介绍了通过扩展方法进行调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

扩展代码

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(',','')。返回结果和intellisense显示了扩展方法,调试器会逐步执行它,就像内置的Split方法一样。

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.

任何想法?

推荐答案

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

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.

您只能做的两件事就是重命名方法或以某种方式更改签名,以使其不同于各种 String.Split 重载。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆