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
propertyDeclaration.Modifiers &= ~Modifiers.Readonly; propertyDeclaration.Modifiers &= ~Modifiers.Readonly;
propertyDeclaration.Getter.Modifiers &= ~Modifiers.Readonly; propertyDeclaration.Getter.Modifiers &= ~Modifiers.Readonly;
// Add C# 7.3 attributes on backing field: var fieldDecl = propertyDeclaration.Parent?.Children.OfType<FieldDeclaration>()
var attributes = field.GetAttributes() .FirstOrDefault(fd => field.Equals(fd.GetSymbol()));
.Where(a => !attributeTypesToRemoveFromAutoProperties.Contains(a.AttributeType.FullName)) if (fieldDecl != null)
.Select(context.TypeSystemAstBuilder.ConvertAttribute).ToArray();
if (attributes.Length > 0)
{ {
var section = new AttributeSection { fieldDecl.Remove();
AttributeTarget = "field" // Add C# 7.3 attributes on backing field:
}; CSharpDecompiler.RemoveAttribute(fieldDecl, KnownAttribute.CompilerGenerated);
section.Attributes.AddRange(attributes); CSharpDecompiler.RemoveAttribute(fieldDecl, KnownAttribute.DebuggerBrowsable);
propertyDeclaration.Attributes.Add(section); 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 // 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
ed.Variables.Add(new VariableInitializer(ev.Name)); ed.Variables.Add(new VariableInitializer(ev.Name));
ed.CopyAnnotationsFrom(ev); 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(); fieldDecl.Remove();
if (field != null) CSharpDecompiler.RemoveAttribute(fieldDecl, KnownAttribute.CompilerGenerated);
CSharpDecompiler.RemoveAttribute(fieldDecl, KnownAttribute.DebuggerBrowsable);
foreach (var section in fieldDecl.Attributes)
{ {
ed.AddAnnotation(field); section.AttributeTarget = "field";
var attributes = field.GetAttributes() ed.Attributes.Add(section.Detach());
.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);
}
} }
} }

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

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

Loading…
Cancel
Save