Method of C Generating DLL File

  • 2021-07-06 11:40:46
  • OfStack

This article illustrates how C # generates DLL files. Share it for your reference. The specific analysis is as follows:

Visual C # Generate DLL files

VisualC, Delphi or VB and other programming languages to write DLL file, after the completion of compilation, DLL file is already a binary file that can be directly used by computers. However, the managed code generated by Visual C # compiler is also a binary file, but it is not the original code that can be directly used by computers. In essence, it is an intermediate language (IL) code, which needs to be compiled by the instant compiler of runtime (JIT) of the next generation window service (Next Generation Windows Services, abbreviated as NGWS).
The DLL file generated with Visual C # is essentially different from the previous DLL file. The DLL files generated by Visual C # are more represented as Category 1 (Class) or Class Library (Class Library) in program design.

Make 1 component

1. Create a new class library project file first
File- > New- > Project- > Visual C# Projects- > Class Library. Fill in the name of the project file and select the directory where the file should be stored.
2. Engineering documents
Rename Class1.cs to the file name you want to create: myDll. cs, and fill in the code.
3. Generate an DLL file
Compile the project file: csc/target: library/out: myDll. DLL myDll. cs generates the component myDll. dll, which will be in the bin\ debug directory of the project file with the file extension dll.

Test DLL

1. Create a new control application
File- > New- > Project- > Visual C# Projects- > Console Application. Test our components with this control application. 2. Add a reference to Namespace
Project- > Add reference, browse to the DLL you just generated, and then press OK. Add the reference to the class of the current project file.
3. Call the myDll namespace, create an myDll object, and call its methods and properties
(1) Use namespace: using myDll;
(2) Creating an myDll object;
(3) Call methods and properties.

DLL Content:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DZTT
{
 public class Operate
 {
  public int getSum(int a, int b)
  {
   return a + b;
  }
 }
}

Usage:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DZTT;
namespace TestDll
{
 class Program
 {
  static void Main(string[] args)
  {
   Operate operate = new Operate();
   int c = operate.getSum(10 ,20);
   Console.WriteLine(c.ToString());
  }
 }
}

I hope this article is helpful to everyone's C # programming.


Related articles: