PropertyDeclaration TransformAutomaticProperties(PropertyDeclaration property) { PropertyDefinition cecilProperty = property.Annotation <PropertyDefinition>(); if (cecilProperty == null || cecilProperty.GetMethod == null || cecilProperty.SetMethod == null) { return(null); } if (!(cecilProperty.GetMethod.IsCompilerGenerated() && cecilProperty.SetMethod.IsCompilerGenerated())) { return(null); } Match m = automaticPropertyPattern.Match(property); if (m != null) { FieldDefinition field = m.Get("fieldReference").Single().Annotation <FieldReference>().ResolveWithinSameModule(); if (field.IsCompilerGenerated() && field.DeclaringType == cecilProperty.DeclaringType) { RemoveCompilerGeneratedAttribute(property.Getter.Attributes); RemoveCompilerGeneratedAttribute(property.Setter.Attributes); property.Getter.Body = null; property.Setter.Body = null; } } // Since the event instance is not changed, we can continue in the visitor as usual, so return null return(null); }
public override void VisitPropertyDeclaration(PropertyDeclaration e) { var pd = e.Annotation<PropertyDefinition>(); if (pd != null) { if (!e.PrivateImplementationType.IsNull && pd.DeclaringType.IsCompilerGenerated()) { // 将接口实现转换为显式实现 var convert = true; if (pd.DeclaringType.Interfaces.Count > 1) { //TagSnapshotRange<T> IEnumerator<TagSnapshotRange<T>>.Current { get { …… } } //object IEnumerator.Current { get { …… } } var trInterface = e.PrivateImplementationType.Annotation<TypeReference>(); var hasSameNameInterfaces = pd.DeclaringType.Interfaces.Select(x => x.Resolve()).Where(x => x == null || x.WellFullName != trInterface.WellFullName && x.Properties.Any(y => y.WellName == pd.WellName)); if (hasSameNameInterfaces.Any()) { if (hasSameNameInterfaces.Any(x => x == null) || !trInterface.HasGenericParameters || hasSameNameInterfaces.Any(x => x.HasGenericParameters)) { convert = false; } } } if (convert) { e.PrivateImplementationType = null; e.Modifiers |= Modifiers.Public; } } var accessor = pd.GetMethod ?? pd.SetMethod; if (accessor != null && accessor.HasOverrides) { // 属性重载或接口属性的显式实现 // bool ICollection.xxx //====> // bool ICollection.IsSynchronized var access = accessor.Overrides.First(); if (access.Name.StartsWith("get_") || access.Name.StartsWith("set_")) { var opn = access.Name.Substring(4); var opd = access.DeclaringType.Resolve().Properties.FirstOrDefault(x => x.Name == opn); if (opd != null) e.Name = opd.WellName; } } else if (e.HasModifier(Modifiers.Override)) { } } base.VisitPropertyDeclaration(e); }