roslyn: Cannot change initonly field outside its .ctor regression
Version Used: 2.6.0.62126 (1758d50b)
Steps to Reproduce:
- Compile following code
struct S
{
public enum Type
{
Error
}
readonly Type type;
public override string ToString ()
{
return type.ToString ();
}
public static void Main ()
{
}
}
- peverify output.exe
Expected Behavior:
No error (as in previous releases)
Actual Behavior:
[IL]: Error: [output.exe : S::ToString][offset 0x00000002] Cannot change initonly field outside its .ctor.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 28 (26 by maintainers)
I started using VS 2017 update 15.7.0 yesterday. I switched a SQL CLR project to using <LangVersion>latest</LangVersion> (for C# 7.3), and I changed several member fields to use readonly per the IDE0044 recommendation. Then I started getting the “Cannot change initonly field outside its .ctor.” error when trying to use SQL’s CREATE ASSEMBLY command. Thankfully, I found this issue, and the fix was just adding <Features>peverify-compat</Features> into my .csproj file.
I wanted to post this here in case anyone else runs into SQL CLR errors with this like I did. My full error message was this:
System.Data.SqlClient.SqlException: CREATE ASSEMBLY for assembly ‘XXX.Sql.Common’ failed because assembly ‘XXX.Sql.Common’ failed verification. Check if the referenced assemblies are up-to-date and trusted (for external_access or unsafe) to execute in the database. CLR Verifier error messages if any will follow this message [ : XXX.Data.XxxCommand::PostExecute][mdToken=0x6000361][offset 0x0000006E] Cannot change initonly field outside its .ctor.
FWIW, I’m using [assembly: AllowPartiallyTrustedCallers] per Microsoft’s recommendation for SQL CLR assemblies. But after going to the C# 7.3 compiler, the peverify-compat feature is required.
I also wanted to mention this here since @jaredpar said above on Oct 4, 2017, “The runtime team is generally skeptical that PEVerify is in significant use.” For anyone using SQL CLR with safe or external_access assemblies, we’re stuck with PEVerify. We may not count as “significant use”, but we don’t want to be left out of using the latest language changes. 😃
@Suchiman Yes, that’s indeed the effort that we’re counting on.
Yes it is
/features:peverify-compat