In C#, we have Reference Assembly, which contains the minimum amount of the metadata that will be needed or required to represent the API surface. So reference assembly includes the declaration for all the members that are useful, but they will exclude all the private me implementation and declarations, which are not providing any impact on the APsurface because it basically provides the metadata to represent the APsurface contract. So we have two types of assemblies here one is implementation, and the other one is reference assemblies, but we cannot use reference assemblies to load for execution, and these types of assemblies are basically distributed with the SDK that stands for Software Development Kit of the library or platform.
Syntax of Assembly Reference in C#
As we know, it is a mechanism that assembles our code into Assembly; for this, we do not have a particular syntax, but we can follow some steps to define assembles in C#. For this, we can use the Reference Manager dialog box to manage and add the reference to components. But if we want to add a reference to the custom components, that have to do manually.
Steps that need to be followed in order to reference the code:
These are some points that need to be in mind while working with reference assemblies in C#; it is more managed by our tools.
As we already know, reference Assembly represents the API surface by providing the metadata about it. So we know about the reference and implementation assembly, but here we will see how it works at the reference assembly and its purpose to use.
1. By the use of reference assembly, we can easily point the reference of a library without having the full and complete implementation for that version. In short, it enables the developers to reference the specific version for that particular library.
2. Let’s understand by the simple example, suppose we have the latest versions for a particular library in a machine. But in a current program, we want to use a specific version which is less than the latest one we have, so in this case, we can make use of reference assembly, which will point to the previous version for that library, but we can get the compile-time error as well.
Now let’s take a look at the structure for the reference assembly in C#:
Reference assembly are also known as the Metadata-only assemblies; they include all the members except the private and anonymous types. Also, their method body is replaced with the throw null; the reason behind using this is to verify and run, pass. We have already known that it removes the private mems from the metadata, which can be useful for referring to an API surface.
The metadata information that it stores to represent the API which is as follows:
Now we can see how we can generate the reference assemblies by using some tools or IDS.
We generate reference libraries because we may have different scenarios where the consumer for that library want to use some different version for a particular program, so reference assemblies help us to achieve this handling for our program because they are very small in size, and also they distribute themselves as the part of the Software development kit only, it also helps us to reduce the download size and helps us to save the disk space as well.
By the use of the below points, we can generate the reference assemblies in C#:
We can follow the below step to add this:
1. goto visual studio and select your project root directory.
2. Reference tab, and you will see the below dialog box on your screen, select dependency you want to add and click ok.
By the use of a reference library, we can handle the latest version and previous version or particular libraries without them to load the fill implementation. Which can help us in several ways like it will save the space in the disk, also increase the performance and so on. Moreover, it is easy to use and build by setting some variable values and passing arguments via the command line.
The above is the detailed content of Assembly Reference in C#. For more information, please follow other related articles on the PHP Chinese website!