Udostępnij za pośrednictwem


/reference (Import Metadata) (C# Compiler Options)

The /reference option causes the compiler to import public (C# Reference) type information in the specified file into the current project, thus enabling you to reference metadata from the specified assembly files.

/reference:[alias=]filename
/reference:filename

Arguments

  • filename
    The name of a file that contains an assembly manifest. To import more than one file, include a separate /reference option for each file.

  • alias
    A valid C# identifier that will represent a root namespace that will contain all namespaces in the assembly.

Remarks

To import from more than one file, include a /reference option for each file.

The files you import must contain a manifest; the output file must have been compiled with one of the /target (Specify Output File Format) (C# Compiler Options) options other than /target:module (Create Module to Add to Assembly) (C# Compiler Options).

/r is the short form of /reference.

Use /addmodule (Import Metadata) (C# Compiler Options) to import metadata from an output file that does not contain an assembly manifest.

If you reference an assembly (Assembly A) that references another assembly (Assembly B), you will need to reference Assembly B if:

  • A type you use from Assembly A inherits from a type or implements an interface from Assembly B.

  • You invoke a field, property, event, or method that has a return type or parameter type from Assembly B.

Use /lib (Specify Assembly Reference Locations) (C# Compiler Options) to specify the directory in which one or more of your assembly references is located. The /lib topic also discusses the directories in which the compiler searches for assemblies.

In order for the compiler to recognize a type in an assembly, and not in a module, it needs to be forced to resolve the type, which you can do by defining an instance of the type. There are other ways to resolve type names in an assembly for the compiler: for example, if you inherit from a type in an assembly, the type name will then be recognized by the compiler.

Sometimes it is necessary to reference two different versions of the same component from within one assembly. To do this, use the alias suboption on the /reference switch for each file to distinguish between the two files. This alias will be used as a qualifier for the component name, and will resolve to the component in one of the files.

The csc response (.rsp) file, which references commonly used .NET Framework assemblies, is used by default. Use /noconfig if you do not want the compiler to use csc.rsp.

Note

In Visual Studio 2008, use the Add Reference dialog box. For more information, see How to: Add or Remove References in Visual Studio.

Example

This example shows how to use the extern alias (C# Reference) feature.

You compile the source file and import metadata from grid.dll and grid20.dll,which have been compiled previously. The two DLLs contain separate versions of the same component, and you use two /reference with alias options to compile the source file. The options look like this:

/reference:GridV1=grid.dll and /reference:GridV2=grid20.dll

This sets up the external aliases "GridV1" and "GridV2," which you use in your program by means of an extern statement:

extern alias GridV1;
extern alias GridV2;
// Using statements go here.

Once this is done, you can refer to the grid control from grid.dll by prefixing the control name with GridV1, like this:

GridV1::Grid

In addition, you can refer to the grid control from grid20.dll by prefixing the control name with GridV2 like this:

GridV2::Grid 

See Also

Other Resources

C# Compiler Options

Project Properties (Visual Studio)