自写扩展方法在 WebMethod 中不可用

发布于 2024-08-26 04:56:48 字数 728 浏览 5 评论 0原文

我为一个 Web 项目编写了一个简单的扩展方法,它位于一个名为 StringExtensions.cs 的类文件中,其中包含以下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Useful extensions for string
/// </summary>
static class StringExtensions
{
    /// <summary>
    /// Is string empty
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static bool IsEmpty(this string value)
    {
        return value.Trim().Length == 0;
    }
}

我可以从该项目中的所有类访问此扩展方法。应用程序_代码目录。但是我有一个名为 JSON.aspx 的网页,其中包含一系列 [WebMethods] - 在这些页面中我看不到扩展方法 - 我一定错过了一些非常明显的东西!

I've written a simple extension method for a web project, it sits in a class file called StringExtensions.cs which contains the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Useful extensions for string
/// </summary>
static class StringExtensions
{
    /// <summary>
    /// Is string empty
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static bool IsEmpty(this string value)
    {
        return value.Trim().Length == 0;
    }
}

I can access this extension method from all the classes I have within the App_Code directory. However I have a web page called JSON.aspx that contains a series of [WebMethods] - within these I cannot see the extension method - I must be missing something very obvious!

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

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

发布评论

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

评论(2

东北女汉子 2024-09-02 04:56:48

为了查看扩展方法,您必须在声明包含扩展方法的类的命名空间中使用 using 指令。

In order to see the extension method you must have an using directive for the namespace in which the class containing the extension method is declared.

却一份温柔 2024-09-02 04:56:48

StringExtensions.cs 文件需要将类声明为 public

以前:

static class StringExtensions{ ... } 

已修复:

public static class StringExtensions{ ... } 

StringExtensions.cs file needed to have the class declared as public

Previously:

static class StringExtensions{ ... } 

Fixed:

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