The process of compiling .cs files into .dll using csc command
Many times, we need to separately compile the .cs file into a .dll file. The operation is as follows:
Open the command window->enter cmd into the console->cd C:WINDOWSMicrosoft.NETFrameworkv1.1.4322
Go to the directory where vs.net is installed -> execute the csc command csc /target:library File.cs -> generate a .dll file with the corresponding name in the directory (prerequisite: put the .cs file in C:WINDOWS Microsoft.NETFrameworkv1.1.4322 directory)
There are many ways to use the csc command, please refer to the following
Translate File.cs to produce File.exe
csc File.cs compiles File.cs to generate File.dll
csc /target:library File.cs Compile File.cs and create My.exe
csc /out:My.exe File.cs Compiles all C# files in the current directory using optimization and defining DEBUG symbols. The output is File2.exe
csc /define:DEBUG /optimize /out:File2.exe *.cs Compiles all C# files in the current directory to generate a debug version of File2.dll. Do not display any logos or warnings
csc /target:library /out:File2.dll /warn:0 /nologo /debug *.cs Compile all C# files in the current directory into Something.xyz (a DLL)
csc /target:library /out:Something.xyz *.cs Compile File.cs to generate File.dll
csc /target:library File.cs is the most commonly used command. In fact, it can be simply written as csc /t:library File.cs. Another way to write it is csc /out:mycodebehind.dll /t:library mycodebehind.cs , you can specify the output file name yourself.
csc /out:mycodebehind.dll /t:library mycodebehind.cs mycodebehind2.cs, the function of this is to install two cs files into one .dll file
Example (taken from the Internet)
1. Dynamic link library
What is a dynamic link library? The three letters DLL must be familiar to you. It is the abbreviation of Dynamic Link Library. A dynamic link library (DLL) is an executable file that is a shared function library. Dynamic linking provides a way for a process to call functions that are not part of its executable code. The executable code for a function is located in a DLL that contains one or more functions that have been compiled, linked, and stored separately from the process that uses them. DLLs also help in sharing data and resources. Multiple applications can access the contents of a single copy of a DLL in memory simultaneously.
Like most programmers, you must have used DLLs frequently. Have you ever felt that it brings you good mistakes in programming and coding? Today I want to discuss a topic with you: how to create and call DLL (dynamic link library) in C#. In fact, in a large sense, DLL It allows me to organize and write our applications more flexibly. As a software designer, I can achieve a high code reuse effect based on it. Let me introduce how to create and call DLL in C#.
2. Preparation work
We need to give a brief introduction to what we are going to do next. In this article we will use the C# language to create a dynamic link library named MyDLL.DLL. In this dynamic link library file we will provide two functions. One is Exchange the values of two parameters. Another function is to find the greatest common divisor of two parameters. Then create an application to use this DLL. Run and output the results.
3. Create DLL
Let's create the following three C# code files:
1. MySwap.cs
}
using System;
namespace MyMethods
{
public class SwapClass
{
public static bool Swap(ref long i,ref long j)
{
i = i+j;
j = ij;
i = ij;
return true;
}
}
}
2. MyMaxCD.cs
using System;
namespace MyMethods
{
public class MaxCDClass
{
public static long MaxCD(long i, long j)
{
long a,b,temp;
if(i>j)
{
a = i;
b = j;
}
else
{
b = i;
a = j;
}
temp = a % b;
while(temp!=0)
{
a = b;
b = temp;
temp = a % b;
}
return b;
}
}
}
It should be noted that when we create these two files, we can use Visual Studio.NET or other text editors, even Notepad. Although these two files are not in the same file, they belong to the same namespace, which will make it convenient for us to use these two methods in the future. Of course, they can also belong to different namespaces, which is completely okay, but we only need to reference two different namespaces when we apply them, so the author recommends that it is better to write them under one namespace.
The next task is to turn these two cs files into the DLL files we need. The method is this: On an operating system with Microsoft.NET Framework installed, we can find the Microsoft.NET directory in the directory where Windows is located. A C# compiler is provided under this directory. Run CSC.EXE: csc /target:library /out:MyDLL.DLL MySwap.cs MyMaxCD.cs. After completion, you can find the MyDLL.DLL file we just generated under this directory. The /target:library compiler option tells the compiler to output DLL files instead of EXE files. The /out compiler option followed by the filename specifies the DLL filename. If /out is not followed by a file name the compiler uses the first file (MySwap.cs) as the DLL file name. The generated file is the MySwap.DLL file.
OK! Our task of creating the dynamic link library file is completed. Now it is time for us to enjoy the fruits of our labor. Below I will introduce how to use the dynamic link library file we created. 4. Using DLL We simply write a small program to test whether the two methods we just wrote are correct. Okay, follow me:
MyClient.cs
using System;
using MyMethods; //Here we refer to the namespace just defined. If we write the two files just now in two different namespaces
class MyClient
{
public static void Main(string[] args)
{
if (args.Length != 2)
{
Console.WriteLine("Usage: MyClient <num1> <num2>");
return;
}
long num1 = long.Parse(args[0]);
long num2 = long.Parse(args[1]);
SwapClass.Swap(ref num1,ref num2);
// Note that the using directive at the beginning of the file allows you to use unqualified class names to reference DLL methods at compile time
Console.WriteLine("The result of swap is num1 = {0} and num2 ={1}",num1, num2);
long maxcd = MaxCDClass.MaxCD(num1,num2);
Console.WriteLine("The MaxCD of {0} and {1} is {2}",num1, num2, maxcd);
}
}
To generate the executable MyClient.exe, use the following command line:
csc /out:MyClient.exe /reference:MyDLL.DLL MyClient.cs
The /out compiler option tells the compiler to output an EXE file and specifies the output file name (MyClient.exe). The /reference compiler option specifies the DLL file that the program references.
5. Execution
To run the program, enter the name of the EXE file followed by two numbers, for example: MyClient 123 456
6. Output
The result of swap is num1 = 456 and num2 = 123
The Max CD of 456 and 123 is 3
7. Summary
Dynamic links have the following advantages:
1. Save memory and reduce swap operations. Many processes can use a DLL simultaneously, sharing a copy of the DLL in memory. Instead, for each application built with a statically linked library, Windows must load a copy of the library code in memory.
2. Save disk space. Many applications can share a copy of the DLL on disk. Instead, each application built with a statically linked library has the library code linked into its executable image as a separate copy. 3. It is easier to upgrade to DLL. When functions in a DLL change, the applications that use them do not need to be recompiled or relinked as long as the function's parameters and return values do not change. In contrast, statically linked object code requires the application to be relinked when functions change.
4. Provide after-sales support. For example, a monitor driver DLL can be modified to support monitors that are not available when the application is originally shipped.
5. Support multi-language programs. Programs written in different programming languages can call the same DLL function as long as the program follows the function's calling convention. Programs and DLL functions must be compatible in the order in which the function expects its arguments to be pushed onto the stack, whether the function or the application is responsible for clearing the stack, and whether any arguments are passed in registers.
6. Provides a mechanism for extending MFC library classes. You can derive classes from existing MFC classes and place them in MFC extension DLLs for use by MFC applications.
7. Make the creation of international versions easy. By placing resources into a DLL, it becomes much easier to create international versions of your application. You can put the strings for each language version of your application into a separate DLL resource file, and have different language versions load the appropriate resources.
One potential disadvantage of using a DLL is that the application is not self-contained; it depends on the presence of a separate DLL module.