如何向 mscorelib 添加方法

发布于 2024-07-27 12:58:20 字数 95 浏览 1 评论 0原文

我想向 mscorlib 添加一些方法。 例如:

字符串abc;

abc.IsNumeric()

我希望能解释我的问题。

I want to add some methods to mscorlib. For example:

string abc;

abc.IsNumeric()

i hope could explain my question.

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

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

发布评论

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

评论(2

素年丶 2024-08-03 12:58:20

您无法向 mscorlib 添加方法,但是您可以使用扩展方法,这样它们看起来就像是在字符串上定义的,例如

public static class StringExtensions
{
    public static bool IsNumeric(this string s)
    {
        // TODO
    }
}

您可以根据需要调用它,例如

"1234".IsNumeric()

You can't add methods to mscorlib, however you can use extension methods so they appear as if they are defined on string, e.g.

public static class StringExtensions
{
    public static bool IsNumeric(this string s)
    {
        // TODO
    }
}

Which you can then call as you requested, e.g.

"1234".IsNumeric()
┊风居住的梦幻卍 2024-08-03 12:58:20

格雷格给了你一个很好的答案。 只是想补充一点,您可以在此处阅读有关扩展方法的更多信息:

You got a good answer by Greg. Just wanted to add that you can read more about extension methods here:

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