RickStrahl / Westwind.Scripting

Small C# library to provide dynamic runtime code compilation from source code for code and expressions execution
209 stars 43 forks source link

Can External References (#r) target netstandard? #22

Closed BrettJaner closed 2 months ago

BrettJaner commented 1 year ago

Hi Rick,

Can external references target netstandard or should they always target the same framework of the app they will be compiled and ran in? You can verify the behavior I'm seeing by when you change the ReferenceTest.csproj to target netstandard2.0 instead of net70. A few of the tests will break.

Thanks, Brett

RickStrahl commented 1 year ago

Top level projects (like Web, Desktop or Test) can't be targeted to .NET Standard - they have to have a concrete runtime to bind to.

Only library projects can target netstandard.

Netstandard assemblies can be referenced from any project type and are loadable. The scripting libray loads Westwind.Utilities which is .net standard, so we know that works.

If it doesn't most likely you need to make sure the depdencies are available - NuGet packages don't publish their dependencies, but you'll need them if your directly reference an assembly.

BrettJaner commented 1 year ago

When I say external references I mean from the scripts perspective, which are scripts that include references at runtime with the #r syntax. When I flip the target framework of the ReferenceTest.csproj from net7.0 to netstandard2.0 and run the tests in Westwind.Scripting.Test.csproj, I see a few failures.

An example from the test CompileClassWithReferenceAndExecute is the following error:

error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

So the 'Object' type would be implemented in net7.0 which the Westwind.Scripting.Test.csproj targets but maybe that same type doesn't satisfy references in netstardard2.0 without some additional help?

RickStrahl commented 1 year ago

This should work just fine as the base libraries from the host can be used. You probably need to add additional base libraries or load the currently running assemblies into the script.

I'll take a look when I have some time, but it may be a bit.