- .NET Core 2.0 By Example
- Rishabh Verma Neha Shrivastava
- 222字
- 2021-06-24 18:31:02
Interop with existing native and Mono libraries
Code reusability is one of the key principles in programming. For example, in an application we may use a certain functionality multiple times, so we keep it in a useful place and refer it from that place, whenever we need it in our application. Now, suppose our application implemented some common functionality which we can use in other applications, in this case, instead of writing the same logic again, we can create a library. It is easily distributable and reusable.
Asp.NET Core supports cross-platform programming, so any Linux user who has built many reusable libraries, API-like console support, and filesystem access, and wants to use them while writing code in ASP.NET Core, can access them. In this section, we will see how to access these native libraries (.dll) and Mono libraries (.so) in the program.
Common Language Infrastructure (CLI) helps to interoperate with existing code. We need to handle three basic problems to get things to work. First, where and how to specify the library, second, determining which function we want to invoke, and third, passing parameters. Common Language Runtime (CLR) provides Platform Invocation services—we call it P/Invoke. It enables managed code to call C, C++ functions in native libraries (DLLs). Let's discuss what Platform Invoke is and how to use it.