diff --git a/ICSharpCode.Decompiler.Tests/RoundtripAssembly.cs b/ICSharpCode.Decompiler.Tests/RoundtripAssembly.cs index 20de6191f..8f79d5b27 100644 --- a/ICSharpCode.Decompiler.Tests/RoundtripAssembly.cs +++ b/ICSharpCode.Decompiler.Tests/RoundtripAssembly.cs @@ -70,7 +70,7 @@ namespace ICSharpCode.Decompiler.Roundtrip [Test] public async Task NRefactory_CSharp() { - await RunWithTest("NRefactory", "ICSharpCode.NRefactory.CSharp.dll", "ICSharpCode.NRefactory.Tests.dll", LanguageVersion.CSharp10_0); + await RunWithTest("NRefactory", "ICSharpCode.NRefactory.CSharp.dll", "ICSharpCode.NRefactory.Tests.dll"); } [Test] diff --git a/ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs b/ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs index d66260563..717e58f0b 100644 --- a/ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs +++ b/ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs @@ -205,6 +205,11 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms if (ctorMethodDef != null && declaringTypeDefinition?.IsReferenceType == false && !declaringTypeDefinition.IsRecord && !isStruct) return; + if (isStruct && !context.Settings.StructFieldInitializers) + { + return; + } + bool ctorIsUnsafe = instanceCtorsNotChainingWithThis.All(c => c.HasModifier(Modifiers.Unsafe)); if (!context.DecompileRun.RecordDecompilers.TryGetValue(declaringTypeDefinition, out var record)) diff --git a/ICSharpCode.Decompiler/DecompilerSettings.cs b/ICSharpCode.Decompiler/DecompilerSettings.cs index 54e4c9e51..f043002f4 100644 --- a/ICSharpCode.Decompiler/DecompilerSettings.cs +++ b/ICSharpCode.Decompiler/DecompilerSettings.cs @@ -150,6 +150,7 @@ namespace ICSharpCode.Decompiler if (languageVersion < CSharp.LanguageVersion.CSharp10_0) { fileScopedNamespaces = false; + structFieldInitializers = false; recordStructs = false; } if (languageVersion < CSharp.LanguageVersion.CSharp11_0) @@ -187,7 +188,7 @@ namespace ICSharpCode.Decompiler return CSharp.LanguageVersion.CSharp12_0; if (scopedRef || requiredMembers || numericIntPtr || utf8StringLiterals || unsignedRightShift || checkedOperators) return CSharp.LanguageVersion.CSharp11_0; - if (fileScopedNamespaces || recordStructs) + if (fileScopedNamespaces || structFieldInitializers || recordStructs) return CSharp.LanguageVersion.CSharp10_0; if (nativeIntegers || initAccessors || functionPointers || forEachWithGetEnumeratorExtension || recordClasses || withExpressions || usePrimaryConstructorSyntax || covariantReturns @@ -464,6 +465,24 @@ namespace ICSharpCode.Decompiler } } + bool structFieldInitializers = true; + + /// + /// Gets/Sets whether C# 10 struct field initializers should be used. + /// + [Category("C# 10.0 / VS 2022")] + [Description("DecompilerSettings.UseStructFieldInitializerSyntax")] + public bool StructFieldInitializers { + get { return structFieldInitializers; } + set { + if (structFieldInitializers != value) + { + structFieldInitializers = value; + OnPropertyChanged(); + } + } + } + bool anonymousMethods = true; /// diff --git a/ILSpy/Properties/Resources.Designer.cs b/ILSpy/Properties/Resources.Designer.cs index 3ddca0ae2..3a8dc6909 100644 --- a/ILSpy/Properties/Resources.Designer.cs +++ b/ILSpy/Properties/Resources.Designer.cs @@ -1632,6 +1632,15 @@ namespace ICSharpCode.ILSpy.Properties { } } + /// + /// Looks up a localized string similar to Use struct field initializer syntax. + /// + public static string DecompilerSettings_UseStructFieldInitializerSyntax { + get { + return ResourceManager.GetString("DecompilerSettings.UseStructFieldInitializerSyntax", resourceCulture); + } + } + /// /// Looks up a localized string similar to Use throw expressions. /// diff --git a/ILSpy/Properties/Resources.resx b/ILSpy/Properties/Resources.resx index ee03a4325..6dd2e996c 100644 --- a/ILSpy/Properties/Resources.resx +++ b/ILSpy/Properties/Resources.resx @@ -564,6 +564,9 @@ Are you sure you want to continue? Use string interpolation + + Use struct field initializer syntax + Use throw expressions diff --git a/ILSpy/Properties/Resources.zh-Hans.resx b/ILSpy/Properties/Resources.zh-Hans.resx index d61d8f79e..1cc08bcd3 100644 --- a/ILSpy/Properties/Resources.zh-Hans.resx +++ b/ILSpy/Properties/Resources.zh-Hans.resx @@ -522,6 +522,9 @@ 使用字符串内插 + + 使用结构字段初始化器语法 + 使用 throw 表达式