views:

567

answers:

2

I created a separate assembly to contain common extension methods, the extension methods uses classes from System.Web.dll (and others).

When I then create a new project (Console Application) that references the Utilities.dll assembly that contains the extension methods, I do not need to add a reference to System.Web.dll to the new project if it does not use the extension methods that extends any class in the System.Web.dll assembly (for example System.Web.UI.Control).

When one of the extension methods will be a generic method everything continues to work as expected. But as soon as I add a constraint to the generic method that constraines it to a class in the System.Web.dll assembly the compiler will complain that my new project (Console Application) needs a reference to System.Web.dll even though the new project is still not using anything in that assembly.

In other words as long as I dont have a constraint on my generic methods everything compiles but as soon as I add a constraint the compiler complains.

An example of my extension methods assemble (compiled as a library Utilities.dll):

public static class StringExtensions
{
    public static bool IsNullOrEmpty(this string value)
    {
        return string.IsNullOrEmpty(value);
    }
}

public static class ControlExtensions
{
    // If I remove the where clause it compiles
    public static T FildChild<T>(this Control parent, string id)
        where T : Control
    {
        throw new NotImplementedException();
    }
}

And here is a new console application that won't compile (unless I also add a reference to System.Web.dll):

    static void Main(string[] args)
    {
        bool isEmpty = "Hello World!".IsNullOrEmpty();

        Console.ReadLine();
    }


Update: As Marc pointed out (below) puting the offending method in a separate namespace fixes the problem.

But the question still remains why is the constraint a problem while the type Control was already used as a parameter to the method. and why is the namespace the solution when I already use the using directive at the top.

+2  A: 

well, yes! In order to compile, it needs to be able to resolve everything in the public/protected API. Otherwise it can't enforce the constraint. I imagine it needs to recognise the types to see if the extension method is a candidate for a method.

You could try placing the extension methods in a child namespace that has "Web" in it - at least then it won't spill into regular code. I've checked, and this fixes the issue. It is good practice to separate the namespaces of extension methods to allow the caller to control when they should be in scope.

In order to execute, it would also need to be able to resolve anything used internally but not exposed in the API. This is standard behavior.

Marc Gravell
I'm not sure I completely understand. 1) My project is not using that type at all. 2) The extension method already uses that Type in the first argument to the method so why does the constraint trigger this madness ?
Y Low
I don't think this answers the question. His code compiles fine even when Control is referenced as a parameter type, but not if he adds it as a constraint *to the same method*. He's not even referencing the Control extensions class.
Mike Scott
Surprisingly placing the offending method in a separate namespace (with any name as long it's not with the other extensions) fixes the problem. Weird at best.
Y Low
But still does not answer the question of why the constraint makes the difference here (the Type is already used before).
Y Low
At a guess, the resolution means it needs to check the constraints... I'd have to look carefully to see if it represents a compiler bug or just expected behavior. Either way, the namespace trick avoids the issue.
Marc Gravell
A: 

@Marc gives you the why. I would suggest that as a good practice you break out anything that refers to web classes into yet another assembly, say Com.Company.Extensions.Web in addition to Com.Company.Extensions. Then you can include both in web projects and only the non-web extensions in other projects.

tvanfosson
Thats exactly how I did it. I have a <company>.WebUtilities.dll assembly
Y Low
BTW I'm not sure that I understand Marc's answer please check my comment.
Y Low