自写扩展方法在 WebMethod 中不可用
我为一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了查看扩展方法,您必须在声明包含扩展方法的类的命名空间中使用
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.StringExtensions.cs 文件需要将类声明为 public
以前:
已修复:
StringExtensions.cs file needed to have the class declared as public
Previously:
Fixed: