Extension methods in a class library project

I implemented some extension methods and put them in a separate class library project.

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

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);
        }
    } 
}

But nowhere in WebApp, for example, in the App_code folder or WebFroms code page, I can not use this extension method. If I like it:

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 = "Someone@Somewhere.com";
        if (email.IsValidEmailAddress())
        {
            //To do 
        }
    }
}

The compiler does not recognize IsValidEmailAddress () and does not even support intellisense.

Although if I put my extension method in the App_Code folder, then it’s normal for use in another cs file in the App_code Folder folders or in a web form.

+3
source share
6

, -?

. , , .

+5

, , . MD.Utility -, " ". , , , - . DLL, DLL , , .

+2

, . : ?

0

, , - , .

, . .

, -. , Public. , Public . , .

0

. , . .

0

In addition to adding the assembly to the links, it was fixed for me to explicitly add it to the file using MD.Utility.

-1
source

Source: https://habr.com/ru/post/1741669/


All Articles