Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 91137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:58:58+00:00 2026-05-10T22:58:58+00:00

I created a separate assembly to contain common extension methods, the extension methods uses

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:58:59+00:00Added an answer on May 10, 2026 at 10:58 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The registered accounts in my web application (Created using struts2) gets a separate site
I'm following the tutorial here . I created a new Web Application. I then
I have created a separate class (let's call it class2.cs for example) and want
Some time back, I created two separate SharePoint projects in VS 2008; one using
I am wondering how to use NUnit correctly. First, I created a separate test
What is the correct way to create a WCF service in separate assembly but
I've created a factory assembly that can process any type of transaction (that supports
So, I created my entity model in a separate class library. I had to
I have an asp.net web app that uses forms-based authentication, a SqlMembershipProvider (using an
We want our web applications separated from our WCF web services. We don't want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.