Updated March 31, 2023
Introduction to Assembly Reference in C#
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:
- First, we have to create a separate folder for each assembly; then, we can move our scripts to those folders that belong to the relevant folder.
- Then we have Unity responsible for taking all the scripts that contain the assembly definition; after that, it will compile them and add the necessary information by using the asset definition.
- Unity will compile all the assemblies for us and identified their dependency as well. So it makes our work less.
These are some points that need to be in mind while working with reference assemblies in C#; it is more managed by our tools.
How does Assembly Reference work in C#?
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:
- It contains all the fields of a structure.
- It also includes all the virtual methods as well.
- It also includes all the types, nested types and private as well.
- It also includes the meta data about the implemented properties and events because their accessors are virtual, if we can see.
- In additional it also includes the details about the attributes.
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 make use of ProduceReferenceAssembly to generate the reference assemblies.
- We can make use of EmitMetadataOnly and IncludePrivateMembers property to set the value and enable the metadata for that. They accept boolean true or false. In this case, to generate the reference assemblies, we have to set them True and False, respectively.
- In C#, when we try to the compiler the program by using the command line to generate the reference assemblies, we can make use of -refonly or else we can use -refout as the compiler options so it will generate the reference assemblies for us.
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.
Conclusion
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.
Recommended Articles
This is a guide to Assembly Reference in C#. Here we discuss the introduction and how assembly reference works in C#? respectively. You may also have a look at the following articles to learn more –