Partager via


How to: Add a Component Reference to a Visual Studio Web Project

In a Visual Studio Web project, you can reference .NET components that are in the global assembly cache (GAC) or COM components registered on your computer. You can also reference components that are located on your hard drive but not in the GAC. Referencing a component makes it available to use in your code.

Note

If you deploy an application that contains a reference to a custom component that is registered in the GAC, the component will not be deployed with the application. To deploy any assembly with your application, you must manually add the assembly to the application's Bin folder. For more information, see Working with Assemblies and the Global Assembly Cache and Deployment and the Global Assembly Cache.

To add references to registered COM components

  1. On the Website menu (for Web site projects), or the Project menu (for Web application projects) choose Add Reference and then click the COM tab.

  2. Select the component you want to use from the list of COM components and then click OK.

    Visual Studio automatically creates an interop assembly, which is a specialized .NET Framework assembly that contains metadata to define COM types and that enables .NET Framework compilers to resolve calls to COM objects.

    If you don't see the component you want, click the Browse tab and look for the component file on your hard drive.

To add references to .NET components that are already registered with the .NET Framework

  1. On the Website menu (for Web site projects), or the Project menu (for Web application projects) choose Add Reference and then click the .NET tab in the dialog box.

  2. Select the component you want to use from the list of .NET components and then click OK.

    If you don't see the component you want, click the Browse tab and look for the assembly file on your hard drive.

To use a referenced component

  • Add an Imports (Visual Basic) statement or using (C#) statement to the top of the class or module and specify the namespace of the component that you have referenced. For more information, see Imports Statement (.NET Namespace and Type) or using Statement (C# Reference).

    You can then use member names without fully qualifying the name (prefixing the member name with the namespace name). For example, if you add a reference to the System.Web.UI.WebControls namespace and you include an Imports (Visual Basic) statement or using (C#) statement for the System.Web.UI.WebControls namespace, you can reference the System.Web.UI.WebControls.SiteMapNodeItem class without using its fully qualified name, as in the following example:

    Imports System.Web.UI.WebControls
    Public Class SampleClass
        Dim smni As SiteMapNodeItem = _
            New SiteMapNodeItem(0, SiteMapNodeItemType.Parent)
    End Class
    
    using System.Web.UI.WebControls;
    public class SampleClass
    {
        SiteMapNodeItem smni = new 
            SiteMapNodeItem(0, SiteMapNodeItemType.Parent);
    }
    

    If you do not use a Visual Basic .NET Imports statement or the C# using statement for the System.Web.UI.WebControls namespace, you can still reference the System.Web.UI.WebControls.SiteMapNodeItem class, but you must use its fully qualified name, as in the following example:

    Public Class SampleClass
        Dim smni As System.Web.UI.WebControls.SiteMapNodeItem = _
            New System.Web.UI.WebControls.SiteMapNodeItem(0, _
            System.Web.UI.WebControls.SiteMapNodeItemType.Parent)
    End Class
    
    public class SampleClass
    {
        System.Web.UI.WebControls.SiteMapNodeItem smni = 
            new System.Web.UI.WebControls.SiteMapNodeItem(0, 
            System.Web.UI.WebControls.SiteMapNodeItemType.Parent);
    }
    

See Also

Tasks

How to: Add or Remove References By Using the Add Reference Dialog Box

How to: Reference COM Objects from Visual Basic

Other Resources

COM Interop (Visual Basic)