views:

44

answers:

2

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}$");
            return regex.IsMatch(s);
        }
    } 
}

But nowhere in WebApp like App_code folder or WebFroms code-behind page I can't use this Extension method. If I do 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 even no intellisense support.

While if I put my extension method in App_Code folder it's ok for using in another cs file in App_code Folder or Web Form code-behind pages.

+2  A: 

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.

driis
There is a reference to classLibrary Project , But i don't know why the Dll don't Update when i rebuild solution , And if i try to Add Reference Again , it prompt that there is already a refrence , When you said It's should be work , I deleted Dlls and added reference again and that worked . Thanks driis . Do you know why my Dll doesn't update automatically ?
Mostafa
That sounds weird. One other thing to check, is that the project is set to build for your current build configuration. On your toolbar you have a dropdown for switching between configurations (debug, release). The last element in that dropdown will bring up a dialog, where you can select or deselect projects for building in the various build configurations.
driis
@Mostafa, you may have been building your library DLL to a different directory than the one you referenced.
Jeff Sternal
+1  A: 

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.

BJ Safdie