Export (0) Print
Expand All

NamespaceCollection Class

Contains a collection of namespace objects. This class cannot be inherited.

Namespace: System.Web.Configuration
Assembly: System.Web (in system.web.dll)

public sealed class NamespaceCollection : ConfigurationElementCollection
public final class NamespaceCollection extends ConfigurationElementCollection
public final class NamespaceCollection extends ConfigurationElementCollection
Not applicable.

The NamespaceCollection contains NamespaceInfo objects. Each NamespaceInfo object is the same as an Import (<%@ Import %>) directive that applies to all pages and controls in the scope of the configuration file. The Import directive allows you to import a namespace into your ASP.NET page, making all its classes available for use on your page.

The following configuration file excerpt shows how to declaratively specify values for several properties of the NamespaceCollection class.

<system.web>
  <pages>
    <namespaces>
      <add namespace="System" />
      <add namespace="System.Collections" />
      <add namespace="System.Collections.Specialized" />
      <add namespace="System.ComponentModel" />
      <add namespace="System.Configuration" />
      <add namespace="System.Web" />
    </namespaces>
  </pages>
</system.web>

The following code example shows how to use the NamespaceCollection type. This code example is part of a larger example provided for the PagesSection class.

// Get the AutoImportVBNamespace property.
Console.WriteLine("AutoImportVBNamespace: '{0}'",
    pagesSection.Namespaces.AutoImportVBNamespace.ToString());

// Set the AutoImportVBNamespace property.
pagesSection.Namespaces.AutoImportVBNamespace = true;
 
// Get all current Namespaces in the collection.
for (int i = 0; i < pagesSection.Namespaces.Count; i++)
{
  Console.WriteLine(
      "Namespaces {0}: '{1}'", i,
      pagesSection.Namespaces[i].Namespace);
}

// Create a new NamespaceInfo object.
System.Web.Configuration.NamespaceInfo namespaceInfo =
    new System.Web.Configuration.NamespaceInfo("System");

// Set the Namespace property.
namespaceInfo.Namespace = "System.Collections";

// Execute the Add Method.
pagesSection.Namespaces.Add(namespaceInfo);

// Add a NamespaceInfo object using a constructor.
pagesSection.Namespaces.Add(
    new System.Web.Configuration.NamespaceInfo(
    "System.Collections.Specialized"));

// Execute the RemoveAt method.
pagesSection.Namespaces.RemoveAt(0);

// Execute the Clear method.
pagesSection.Namespaces.Clear();

// Execute the Remove method.
pagesSection.Namespaces.Remove("System.Collections");

// Get the current AutoImportVBNamespace property value.
Console.WriteLine(
    "Current AutoImportVBNamespace value: '{0}'",
    pagesSection.Namespaces.AutoImportVBNamespace);

// Set the AutoImportVBNamespace property to false.
pagesSection.Namespaces.AutoImportVBNamespace = false;

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft