Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify Framework Version for CSharpCompilation.Emit

Does anyone know whether it is possible to specify the Framework-Version when Emitting assemblies using CSharpCompilation's extensionmethod Emit?

We need to target 4.5.1 apparently since otherwise we get a nasty runtime error:

MissingMethodException
Method not found: '!!0[] System.Array.Empty()'.

Or do we have to wait till clients are upgraded to Framework 4.6 before we can make use of Roslyn?

like image 960
CSharpie Avatar asked Jan 08 '23 00:01

CSharpie


1 Answers

The compiler doesn't (and has never) had a concept of "framework version". All the compiler understands is what set of references are included. It sounds like you are creating your compilation with references to 4.6 versions of the framework (possibly because you are using running assembly locations, and are running on 4.6).

Instead, you'll need to ensure that you have the 4.5.1 targeting pack installed (VS install does this automatically, but other machines may not have it), and add its references to your compilation from out of C:\Program Files (x86)\Reference Assemblies.

The compiler will emit references to Array.Empty if it exists in the referenced assemblies, but has fallback logic if it doesn't.

like image 153
Kevin Pilch Avatar answered Jan 20 '23 22:01

Kevin Pilch