Hello,
Welcome to Microsoft Q&A!
You couldn't directly use a C# class library in your C++ application. You need to use a C# Windows Runtime Component instead. And you will need to do some extra steps to make sure the C# component is compiled correctly with the C++ application.
Here are the steps to create a simple test sample:
- Create a new "Blank App (Universal Windows - C++/CX)"
- Create a new "Windows Runtime Component (Universal Windows)", C#, Called
TestWinRTLib
in the solution. - Add a simple test method in the Class1.cs:
public static string GetName() { return "David"; }
- Add referece of the
TestWinRTLib
in the C++ project. And call the method like this:using namespace TestWinRTLib; void TestCXProJECT::MainPage::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) { auto str = Class1::GetName(); }
- Right click on the C++/CX project, choose Manage NuGet Packages.
- Search for Microsoft.Net.Native.Compiler, and install it
- Right-click the C++/CX project -> Unload Project -> Edit .vcxproj, add following properties to the PropertyGroup. Remember to replace the 2.2.3 version above with the version of Microsoft.Net.Native.Compiler nuget package you installed
<PropertyGroup Label="Globals"> <-- other properties --> <UseDotNetNativeToolchain>true</UseDotNetNativeToolchain> <DotNetNativeVersion>2.2.3</DotNetNativeVersion> </PropertyGroup>
- Reload the C++/CX project first, then right click the Solution, choose Restore NuGet Packages
- Run the C++ app and click the button, you could get string from the C# Component.
The result looks like this:
You could refer these this as well: Could not load file or assembly.
Thank you.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.