Understanding References

The Base Class Library (BCL) exposes types through several assemblies that are part of the .NET Framework, and you will often need to invoke types from those assemblies. Moreover, although it is very rich, the BCL cannot define types covering every aspect of application development. This means you will often need to use types exposed by other assemblies, such as other projects in the same solution or external compiled assemblies.


Note

Each time you create a new Windows client project (via Windows Forms, WPF, Console), Visual Studio automatically adds references to some .NET assemblies (such as System.dll and System.Core.dll) that are necessary for each kind of application and that expose the BCL’s core part. In Visual Studio 2015, this changes for ASP.NET Core and for Universal Windows apps for Windows 10, where the IDE downloads and references only the necessary libraries instead of requiring the full .NET Framework.


To use types defined in external assemblies, you need to add to your project a reference to the desired assembly. To accomplish this, right-click the project name in Solution Explorer and click the Add Reference command from the context menu or select the References tab in My Project and click Add. Either way, you open the Reference Manager dialog box, as shown in Figure 3.3.

Image

FIGURE 3.3 The Reference Manager dialog box.

You can select all the assemblies you want to reference; multiple selections are allowed by clicking the check box near the assembly name or by pressing Ctrl and then clicking the name of the required assembly. The Reference Manager dialog box is divided into several tabs. The default tab, Assemblies, shows a list of all the available assemblies in the global assembly cache through a group called Framework, and it shows a list of available assemblies exposed by installed extensions for Visual Studio through a group called Extensions.


What is the Global Assembly Cache?

The global assembly cache (GAC) can be described as a repository for information and locations on installed assemblies. The .NET Framework knows where assemblies can be found by browsing the GAC, which also can distinguish between different versions of an assembly. The GAC is discussed in detail in Chapter 48, “Understanding the Global Assembly Cache.”


The Reference Manager dialog box shows the version numbers of assemblies; this is useful because you can have different versions of an assembly with the same name. When you add a reference to an assembly, Solution Explorer updates the References node. For example, to add security features to your applications, you need to add a reference to the System.Security.dll assembly (refer to Figure 3.3), which is part of the BCL. When this is added, Solution Explorer looks as shown in Figure 3.4.

Image

FIGURE 3.4 Solution Explorer is updated with the new reference.

You can use the code types that are exposed by the specified assemblies that have public visibility. New in Visual Basic 2015 is the automatic availability of the References node in Solution Explorer; you can simply expand this node to see a list of currently referenced assemblies or projects. In earlier versions of VB, you had to enable the All Files view to get this result.

The Reference Manager dialog box also provides other tabs and groups:

Image The Recent group in the Assemblies tab shows a list of all the most recently used assemblies for faster reuse.

Image The Browse tab enables you to search for assemblies that are not registered in the GAC.

Image The Solution tab enables you to add references to other projects in the solution. This is typically the case when you have a class library that exposes types you want to use inside a client application.

Image A new tab called Shared Projects allows you to add references to Shared Projects, a new feature in Visual Basic 2015 that allows you to share code between different project types (see Chapter 17, “Working with Objects: Visual Tools and Code Sharing”).

Image The COM tab lets you add references to COM-type libraries, as discussed next.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.44.229