Freigeben über


How to: Add a Reference to a .NET or COM Component in a Web Site

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. In previous versions of Visual Studio, you could set the Copy Local property for a reference, which ensured that the assembly would be deployed. In ASP.NET 2.0 and later versions, to deploy any assembly with your application, you must manually add the assembly to the application's Bin folder. Doing so reduces the risk of publishing custom code that you are not familiar with. 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, 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 Basic automatically creates an interop assembly, which is a specialized .NET assembly that contains metadata to define COM types and that enables .NET 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, 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 that identifies the namespace to reference. 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 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 Import statement or the C# using statement for the System.Web.UI.WebControls namespace, you 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 in Visual Studio (Visual Basic)

How to: Reference COM Objects from Visual Basic

Other Resources

COM Interop