开发者

When is a .NET namespace implemented by a .NET Framework component?

(Yet another question from my "Clearly I'm the only idiot out here" series.)

When I need to use a class from the .NET Framework, I dutifully look up the documentation to determine the corresponding namespace and then add a "using" directive to my source code:

using System.Text.RegularExpressions;

Usually I'm good to go at this point, but sometimes Intellisense doesn't recognize the new class and the project won't build. A quick check in the Object Browser confirms that I have the right namespace. Frustration ensues.

Using HttpUtility.UrlEncode() involved adding the appropriate directive:

using System.Web;

But it also required adding a reference to .NET Framework Component for System.Web, i.e. right-click the project in Solution Explorer, select Add Reference and add System.Web from the .NET tab.

How might I discern from the documentation whether a .NET namespace is implemented by a .NET Framework Component that must be referenced? I'd rather n开发者_StackOverflow中文版ot hunt through the available components every time I use a namespace on the off chance that a reference is needed.

(For those who like to stay after class and clean the erasers: Will Organize Usings > Remove and Sort also remove references to componenents that are not used elsewhere in the project? How do you clean up unnecessary references?)


Check out this link for UrlEncode:

Namespace: System.Web

Assembly: System.Web (in System.Web.dll)

The Assembly line tells you which dll to reference.


You'll note that the documentation (e.g. http://msdn.microsoft.com/en-us/library/system.web.httputility.aspx) tells you the name of the assembly/DLL that the class should be found in, along with the class's namespace.

Namespace: System.Web
Assembly: System.Web (in System.Web.dll)

On a side note, I know it can be a little dear, but Resharper makes things like this so much easier. If you're a serious developer, you may want to consider investing in a license. For the eraser-cleaners, Resharper adds a handy little "Find Code Dependent on Module" item to the right-click menu on references in the Solution Explorer. It's not quite an automatic cleanup, but it makes it a lot easier to see whether something's still being used by your project.


The documentation specifies two things for any type:

  • The namespace of the type (for the using directive)
  • The assembly containing the type (this is what you add a reference to)

To take an example where the two are different, look at the documentation for Enumerable:

Namespace: System.Linq
Assembly: System.Core (in System.Core.dll)


If you look at the MSDN docs, e.g.

http://msdn.microsoft.com/en-us/library/system.web.httputility.aspx

It tells you the namespace and the assembly that is required.


First, a correction to your terminology: What you are referencing is called an "assembly". An assembly contains classes that belong to a namespace. A namespace can span across multiple assemblies.

Most assemblies are named the same as the main namespace that is contained in them. For example, System.Web exists in System.Web.dll. The documentation also usually tells you which assembly needs to be referenced.


I think that you are running into a difference between c# and C here. To compare: In C, all you need to do to include a new library is to include it in the header.

It .net, you need to be aware of 2 things:

  1. A namespace can span more than one assembly/dll (that means that you might not get a compiler error on the using clause, because some of the dlls that support that namespace are referenced -- just not the one that you need)
  2. To "see" the contents of a given assembly, you have to add a reference to it. The using clause alone just gives you some short-cut syntax so that you can write HttpUtility.Encode(), instead of System.Web.HttpUtility.UrlEncode(), you have to add the reference in order for the compiler to "know" about the class.

To avoid your problem:

In the MSDN documents, pay attention to the assembly that the class is in, and make sure that you have a reference to the assembly.


If you browse to the MSDN for the class you're trying to use. It typically tells you the assembly that the class is in. For example the Regex class is in Assembly System (in System.dll) or the HttpUtility class is in Assembly System.Web (in System.Web.dll).

I believe tools such as ReSharper help with this, as well, and automatically references the assemblies you need.

I'm pretty sure you have to manually remove the unused references in C# projects. In VB.NET projects there's a button to list the unused references when you're in the project properties page. I don't see this in C# projects, though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜