Native dependencies
Since we are running WebAssembly, we can use WebAssembly assemblies written in other languages in our project. This means that we can use any native dependencies right inside our project.
One way is to add C files right into our project. In the Chapter16
folder in the repository, you will find an example.
I have added a file called Test.c
with the following content:
int fact(int n)
{
if (n == 0) return 1;
return n * fact(n - 1);
}
In the project file, I have added a reference to that file:
<ItemGroup>
<NativeFileReference Include="Test.c" />
</ItemGroup>
In Home.razor
, I have added the following code:
@page "/"
@using System.Runtime.InteropServices
<PageTitle>Native C</PageTitle>
<h1>Native C Test</h1>
<p>
@@fact(3) result: @fact(3)
</p>
@code {
[DllImport("Test")]
static extern int fact(int n);
}
In our C# project, we now...