类库项目中的扩展方法

发布于 2024-08-29 11:19:12 字数 1244 浏览 7 评论 0原文

我已经实现了一些扩展方法并将它们放在单独的类库项目中。

想象一下,我在名为 MD.Utility 的类库中有一个像这样的简单扩展方法:

namespace MD.Utility
{
    public static class ExtenMethods
    {
        public static bool IsValidEmailAddress(this string s)
        {
            Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
            return regex.IsMatch(s);
        }
    } 
}

但在 Web 应用程序中没有像 App_code 文件夹或WebFroms代码隐藏页面可以使用这个扩展方法。如果我这样做:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MD.Utility;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string email = "[email protected]";
        if (email.IsValidEmailAddress())
        {
            //To do 
        }
    }
}

编译器无法识别 IsValidEmailAddress() 并且甚至没有 IntelliSense 支持。

如果我将扩展方法放在 App_Code 文件夹中,则它可以在 App_code 文件夹或 WebForms 代码隐藏中的另一个 .cs 文件中使用页。

I've implemented some extension methods and put those in separate Class Library project.

Imagine I have a simple extension method like this in class library called MD.Utility:

namespace MD.Utility
{
    public static class ExtenMethods
    {
        public static bool IsValidEmailAddress(this string s)
        {
            Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}
quot;);
            return regex.IsMatch(s);
        }
    } 
}

But nowhere in the web app like the App_code folder or the WebFroms code-behind page can I use this extension method. If I do something like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MD.Utility;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string email = "[email protected]";
        if (email.IsValidEmailAddress())
        {
            //To do 
        }
    }
}

The compiler doesn't recognize IsValidEmailAddress() and there's even no IntelliSense support.

While if I put my extension method in the App_Code folder, it's usable in another .cs files in the App_code folder or the WebForms code-behind pages.

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

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

发布评论

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

评论(5

岛歌少女 2024-09-05 11:19:12

您是否记得在 Web 项目中添加对类库的引用?

你会需要那个。除此之外,您的代码看起来不错,并且应该可以工作。

Did you remember to add a reference to your class library in the web project ?

You will need that. Other than that, your code looks fine, and should work.

浅黛梨妆こ 2024-09-05 11:19:12

如果在重建解决方案时未重新编译更改,则可能是您正在使用的引用类型所致。如果 MD.Utility 项目位于您的 Web 项目解决方案中,则应将引用设为“项目引用”。这将导致构建将该代码视为依赖项,因此在您更改某些内容时重建它。如果您只是将其作为 DLL 包含,则该 DLL 被视为外部并且构建不会考虑它,即使它位于同一解决方案中。

If changes are not getting recompiled when you do a solution rebuild, then it could be the type of reference you are using. If the MD.Utility project is in your web project solution, you should make the reference a "Project Reference." That will cause the build to consider that code as a dependency and therefore rebuild it when you change something. If you just include it as a DLL, then the DLL is considered external and the build will not consider it, even if it is in the same solution.

燃情 2024-09-05 11:19:12

我能够通过公开扩展模块来解决这个问题。
这篇文章可能会有所帮助:
引用程序集中的扩展方法?

I was able to resolve it by making the extension module public.
This post may be helpful:
Extension methods in referenced assemblies?

傻比既视感 2024-09-05 11:19:12

除了将程序集添加到引用之外,对我来说修复它的方法是将其显式添加到文件“使用 MD.Utility”。

In addition to adding the assembly to the references, what fixed it for me was to explicitly adding it to the file "using MD.Utility".

眼角的笑意。 2024-09-05 11:19:12

我发现如果使用项目的程序集名称和命名空间相同并且公共库具有相同的命名空间,则可能会发生这种情况。

编译器似乎感到困惑。尝试改变它们。

如其他地方所述,您需要将公共库添加到每个使用项目中。并且公共库中包含扩展的模块必须标记为公共。与类不同,Public 不是模块的默认范围。不知道为什么。

I've found that this can occur if the Assembly Name and Namespace of the consuming project are the same and the Common library has the same Namespace.

Seems that the compiler gets confused. Try changing them.

As noted elsewhere, you need to add the Common library to each consuming project. And the Module containing the Extension(s) in the Common library must be marked Public. Unlike Classes, Public isn't the default scope for Modules. No idea why.

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