I'm currently trying to create a plugin with GUI (WPF) for Solidworks using the Solidworks API. I created the project with visual Studio 2019 using .net-core 3.1. This API mostly uses functions with returntype dynamic. I used this code before for a console application (also using .net-core 3.1) wihtout getting any building-errors.
using SW = SldWorks;
using System.Windows;
namespace dfdfdf
{
public partial class App : Application
{
private void App_Startup(object sender, StartupEventArgs e)
{
//Connect to SW
SW.SldWorks SolidWorksApp = null;
SW.ModelDoc2 swDoc;
SolidWorksApp = new SldWorks.SldWorks();
//Connect to Document --> Build Error
swDoc = SolidWorksApp.ActiveDoc;
}
}
}
Now with the WPF project I get the following error:
The error is only shown when "only build" is selected on the error list
CS2066 (Cannot implicitly convert type ‘type1’ to ‘type2’. An explicit conversion exists (are you missing a cast?))
"ActiveDoc" has a dynamic as return-value and using the explicit conversion works fine:
swDoc = (SW.ModelDoc2) SolidWorksApp.ActiveDoc;
but I'm wondering why this causes an error in a WPF application but not in a console application. As far as I understand it I shouldn't get a build errors at all when casting a dynamic since the type-conversion is done on runtime and not on buildtime.
For the COM-References i activated the option "embedded interoptypes", is there any other option I missed that causes this behaviour?
If .core needs the explicit casts I'm fine with that. I just hope one of you guys can tell me why there is a difference between wpf and console applications regarding this.
Thanks!
Check this video, but I would strongly recommend against .NET Core/.NET5-6 for SOLIDWORKS add-ins due to many compatibility issues. Use.NET Framework instead for add-ins and it is OK to use .NET Core for stand-alone applications.