Browse Source

Remove backing fields of auto properties and events.

The previous commit started decompiling backing fields because they are still needed prior to the C# transforms.
pull/2684/head
Siegfried Pammer 3 years ago
parent
commit
40ffc1e90e
  1. 46
      ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs
  2. 2
      ICSharpCode.Decompiler/TypeSystem/Implementation/KnownAttributes.cs

46
ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs

@ -640,17 +640,19 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -640,17 +640,19 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
propertyDeclaration.Modifiers &= ~Modifiers.Readonly;
propertyDeclaration.Getter.Modifiers &= ~Modifiers.Readonly;
// Add C# 7.3 attributes on backing field:
var attributes = field.GetAttributes()
.Where(a => !attributeTypesToRemoveFromAutoProperties.Contains(a.AttributeType.FullName))
.Select(context.TypeSystemAstBuilder.ConvertAttribute).ToArray();
if (attributes.Length > 0)
var fieldDecl = propertyDeclaration.Parent?.Children.OfType<FieldDeclaration>()
.FirstOrDefault(fd => field.Equals(fd.GetSymbol()));
if (fieldDecl != null)
{
var section = new AttributeSection {
AttributeTarget = "field"
};
section.Attributes.AddRange(attributes);
propertyDeclaration.Attributes.Add(section);
fieldDecl.Remove();
// Add C# 7.3 attributes on backing field:
CSharpDecompiler.RemoveAttribute(fieldDecl, KnownAttribute.CompilerGenerated);
CSharpDecompiler.RemoveAttribute(fieldDecl, KnownAttribute.DebuggerBrowsable);
foreach (var section in fieldDecl.Attributes)
{
section.AttributeTarget = "field";
propertyDeclaration.Attributes.Add(section.Detach());
}
}
}
// Since the property instance is not changed, we can continue in the visitor as usual, so return null
@ -1010,23 +1012,17 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -1010,23 +1012,17 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
ed.Variables.Add(new VariableInitializer(ev.Name));
ed.CopyAnnotationsFrom(ev);
if (ev.GetSymbol() is IEvent eventDef)
var fieldDecl = ev.Parent?.Children.OfType<FieldDeclaration>()
.FirstOrDefault(fd => fd.Variables.Single().Name == ev.Name);
if (fieldDecl != null)
{
IField field = eventDef.DeclaringType.GetFields(f => f.Name == ev.Name, GetMemberOptions.IgnoreInheritedMembers).SingleOrDefault();
if (field != null)
fieldDecl.Remove();
CSharpDecompiler.RemoveAttribute(fieldDecl, KnownAttribute.CompilerGenerated);
CSharpDecompiler.RemoveAttribute(fieldDecl, KnownAttribute.DebuggerBrowsable);
foreach (var section in fieldDecl.Attributes)
{
ed.AddAnnotation(field);
var attributes = field.GetAttributes()
.Where(a => !attributeTypesToRemoveFromAutoEvents.Contains(a.AttributeType.FullName))
.Select(context.TypeSystemAstBuilder.ConvertAttribute).ToArray();
if (attributes.Length > 0)
{
var section = new AttributeSection {
AttributeTarget = "field"
};
section.Attributes.AddRange(attributes);
ed.Attributes.Add(section);
}
section.AttributeTarget = "field";
ed.Attributes.Add(section.Detach());
}
}

2
ICSharpCode.Decompiler/TypeSystem/Implementation/KnownAttributes.cs

@ -48,6 +48,7 @@ namespace ICSharpCode.Decompiler.TypeSystem @@ -48,6 +48,7 @@ namespace ICSharpCode.Decompiler.TypeSystem
SpecialName,
DebuggerHidden,
DebuggerStepThrough,
DebuggerBrowsable,
// Assembly attributes:
AssemblyVersion,
@ -124,6 +125,7 @@ namespace ICSharpCode.Decompiler.TypeSystem @@ -124,6 +125,7 @@ namespace ICSharpCode.Decompiler.TypeSystem
new TopLevelTypeName("System.Runtime.CompilerServices", nameof(SpecialNameAttribute)),
new TopLevelTypeName("System.Diagnostics", nameof(DebuggerHiddenAttribute)),
new TopLevelTypeName("System.Diagnostics", nameof(DebuggerStepThroughAttribute)),
new TopLevelTypeName("System.Diagnostics", nameof(DebuggerBrowsableAttribute)),
// Assembly attributes:
new TopLevelTypeName("System.Reflection", nameof(AssemblyVersionAttribute)),
new TopLevelTypeName("System.Runtime.CompilerServices", nameof(InternalsVisibleToAttribute)),

Loading…
Cancel
Save