Skip to content

Commit 0c2b001

Browse files
Merge pull request #3540 from miloush/AlwaysMoveInitializer
Always move initializer decompilation setting
2 parents 582e847 + 9d11f52 commit 0c2b001

File tree

5 files changed

+52
-16
lines changed

5 files changed

+52
-16
lines changed

ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ void HandleStaticFieldInitializers(IEnumerable<AstNode> members)
350350
if (!(fieldOrProperty is IField || fieldOrProperty is IProperty) || !fieldOrProperty.IsStatic)
351351
break;
352352
// Only move fields that are constants, if the declaring type is not marked beforefieldinit.
353-
if (!declaringTypeIsBeforeFieldInit && fieldOrProperty is not IField { IsConst: true })
353+
if (!context.Settings.AlwaysMoveInitializer && !declaringTypeIsBeforeFieldInit && fieldOrProperty is not IField { IsConst: true })
354354
{
355355
pos++;
356356
continue;

ICSharpCode.Decompiler/DecompilerSettings.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2274,6 +2274,27 @@ public bool AlwaysUseGlobal {
22742274
}
22752275
}
22762276

2277+
bool alwaysMoveInitializer = false;
2278+
2279+
/// <summary>
2280+
/// If set to false (the default), the decompiler will move field initializers at the start of constructors
2281+
/// to their respective field declrations (TransformFieldAndConstructorInitializers) only when the declaring
2282+
/// type has BeforeFieldInit or the member IsConst.
2283+
/// If set true, the decompiler will always move them regardless of the flags.
2284+
/// </summary>
2285+
[Category("DecompilerSettings.Other")]
2286+
[Description("DecompilerSettings.AlwaysMoveInitializer")]
2287+
public bool AlwaysMoveInitializer {
2288+
get { return alwaysMoveInitializer; }
2289+
set {
2290+
if (alwaysMoveInitializer != value)
2291+
{
2292+
alwaysMoveInitializer = value;
2293+
OnPropertyChanged();
2294+
}
2295+
}
2296+
}
2297+
22772298
bool sortCustomAttributes = false;
22782299

22792300
/// <summary>

ILSpy/Properties/Resources.Designer.cs

Lines changed: 23 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ILSpy/Properties/Resources.resx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ Are you sure you want to continue?</value>
264264
<data name="DecompilerSettings.AlwaysCastTargetsOfExplicitInterfaceImplementationCalls" xml:space="preserve">
265265
<value>Always cast targets of explicit interface implementation calls</value>
266266
</data>
267+
<data name="DecompilerSettings.AlwaysMoveInitializer" xml:space="preserve">
268+
<value>Always move field initializers from constructors to declarations</value>
269+
</data>
267270
<data name="DecompilerSettings.AlwaysQualifyMemberReferences" xml:space="preserve">
268271
<value>Always qualify member references</value>
269272
</data>
@@ -361,7 +364,7 @@ Are you sure you want to continue?</value>
361364
<value>Transform to do-while, if possible</value>
362365
</data>
363366
<data name="DecompilerSettings.ExpandParamsArguments" xml:space="preserve">
364-
<value>Expand params arguments by removing explicit array creation</value>
367+
<value>Expand params arguments by removing explicit array creation</value>
365368
</data>
366369
<data name="DecompilerSettings.FSpecificOptions" xml:space="preserve">
367370
<value>F#-specific options</value>

ILSpy/Properties/Resources.zh-Hans.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@
258258
<data name="DecompilerSettings.AlwaysCastTargetsOfExplicitInterfaceImplementationCalls" xml:space="preserve">
259259
<value>始终强制转换显式接口实现调用的目标</value>
260260
</data>
261+
<data name="DecompilerSettings.AlwaysMoveInitializer" xml:space="preserve">
262+
<value />
263+
</data>
261264
<data name="DecompilerSettings.AlwaysQualifyMemberReferences" xml:space="preserve">
262265
<value>始终限定成员引用</value>
263266
</data>

0 commit comments

Comments
 (0)