private void WritePropertyName(IPropertyDefinition property, IMethodDefinition accessor, bool isSetterAccessor, bool isIndexer) { if (property.IsExplicitInterfaceProperty()) { IMethodImplementation methodImplementation = accessor.GetMethodImplementation(); object nullableAttributeArgument = methodImplementation.GetExplicitInterfaceMethodNullableAttributeArgument(_metadataReaderCache); if (nullableAttributeArgument != null) { WriteTypeName(methodImplementation.ImplementedMethod.ContainingType, noSpace: true, nullableAttributeArgument: nullableAttributeArgument); WriteSymbol("."); if (isIndexer) { WriteIdentifier("this", false); WriteIndexerParameters(accessor, isSetterAccessor); } else { string name = methodImplementation.ImplementedMethod.Name.Value; WriteIdentifier(name.Substring(name.IndexOf("_") + 1)); } return; } } if (isIndexer) { int index = property.Name.Value.LastIndexOf("."); if (index >= 0) { WriteIdentifier(property.Name.Value.Substring(0, index + 1) + "this", false); // +1 to include the '.' } else { WriteIdentifier("this", false); } WriteIndexerParameters(accessor, isSetterAccessor); } else { WriteIdentifier(property.Name); } }
private void WritePropertyDefinition(IPropertyDefinition property) { bool isInterfaceProp = property.ContainingTypeDefinition.IsInterface; IMethodDefinition accessor = null; IMethodDefinition getter = null; IMethodDefinition setter = null; if (property.Getter != null) { getter = property.Getter.ResolvedMethod; if (!_filter.Include(getter)) { getter = null; } accessor = getter; } if (property.Setter != null) { setter = property.Setter.ResolvedMethod; if (!_filter.Include(setter)) { setter = null; } if (accessor == null) { accessor = setter; } } if (accessor == null) { return; } bool isIndexer = accessor.ParameterCount > (accessor == setter ? 1 : 0); if (isIndexer) { string id = property.Name.Value; int index = id.LastIndexOf("."); if (index >= 0) { id = id.Substring(index + 1); } if (id != "Item") { WriteFakeAttribute("System.Runtime.CompilerServices.IndexerName", "\"" + id + "\""); } } WriteAttributes(property.Attributes); // We need to preserve nullable custom attributes which are preserved by the compiler as param on the value parameter and return attributes. if (getter != null) { WriteAttributes(getter.ReturnValueAttributes); } if (setter != null) { WriteAttributes(setter.Parameters.Last().Attributes); } if (!isInterfaceProp) { if (!accessor.IsExplicitInterfaceMethod()) { WriteVisibility(property.Visibility); } // Getter and Setter modifiers should be the same WriteMethodModifiers(accessor); } if (property.GetHiddenBaseProperty(_filter) != Dummy.Property) { WriteKeyword("new"); } bool getterHasIsReadOnlyAttribute = (getter?.Attributes.HasIsReadOnlyAttribute()).GetValueOrDefault(); bool setterHasIsReadOnlyAttribute = (setter?.Attributes.HasIsReadOnlyAttribute()).GetValueOrDefault(); // The readonly modifier is applied on the property itself if: // * It has both a getter and a setter and both have IsReadOnlyAttribute // * It only has a getter or a setter and it has IsReadOnlyAttribute // Otherwise, the attribute is applied directly on the getter/setter it exists for bool allAccessorsHaveIsReadOnlyAttribute = (getterHasIsReadOnlyAttribute && setterHasIsReadOnlyAttribute) || (getterHasIsReadOnlyAttribute && (setter is null)) || (setterHasIsReadOnlyAttribute && (getter is null)); if (allAccessorsHaveIsReadOnlyAttribute && (LangVersion >= LangVersion8_0)) { WriteKeyword("readonly"); } if (property.ReturnValueIsByRef) { WriteKeyword("ref"); if (property.Attributes.HasIsReadOnlyAttribute()) { WriteKeyword("readonly"); } } WriteTypeName(property.Type, attributes: property.Attributes); if (property.IsExplicitInterfaceProperty() && _forCompilationIncludeGlobalprefix) { Write("global::"); } WritePropertyName(property, accessor, accessor == setter, isIndexer); WriteSpace(); WriteSymbol("{"); //get if (getter != null) { bool isReadOnly = getterHasIsReadOnlyAttribute && !allAccessorsHaveIsReadOnlyAttribute; WriteAccessorDefinition(property, getter, "get", isReadOnly); } //set if (setter != null) { bool isReadOnly = setterHasIsReadOnlyAttribute && !allAccessorsHaveIsReadOnlyAttribute; WriteAccessorDefinition(property, setter, "set", isReadOnly); } WriteSpace(); WriteSymbol("}"); }
private void WritePropertyDefinition(IPropertyDefinition property) { bool isInterfaceProp = property.ContainingTypeDefinition.IsInterface; IMethodDefinition accessor = null; IMethodDefinition getter = null; IMethodDefinition setter = null; if (property.Getter != null) { getter = property.Getter.ResolvedMethod; if (!_filter.Include(getter)) { getter = null; } accessor = getter; } if (property.Setter != null) { setter = property.Setter.ResolvedMethod; if (!_filter.Include(setter)) { setter = null; } if (accessor == null) { accessor = setter; } } if (accessor == null) { return; } bool isIndexer = accessor.ParameterCount > (accessor == setter ? 1 : 0); if (isIndexer) { string id = property.Name.Value; int index = id.LastIndexOf("."); if (index >= 0) { id = id.Substring(index + 1); } if (id != "Item") { WriteFakeAttribute("System.Runtime.CompilerServices.IndexerName", "\"" + id + "\""); } } WriteAttributes(property.Attributes); if (!isInterfaceProp) { if (!accessor.IsExplicitInterfaceMethod()) { WriteVisibility(property.Visibility); } // Getter and Setter modifiers should be the same WriteMethodModifiers(accessor); } if (property.GetHiddenBaseProperty(_filter) != Dummy.Property) { WriteKeyword("new"); } if (property.ReturnValueIsByRef) { WriteKeyword("ref"); if (property.Attributes.HasIsReadOnlyAttribute()) { WriteKeyword("readonly"); } } WriteTypeName(property.Type, isDynamic: IsDynamic(property.Attributes)); if (property.IsExplicitInterfaceProperty() && _forCompilationIncludeGlobalprefix) { Write("global::"); } if (isIndexer) { int index = property.Name.Value.LastIndexOf("."); if (index >= 0) { WriteIdentifier(property.Name.Value.Substring(0, index + 1) + "this", false); // +1 to include the '.' } else { WriteIdentifier("this", false); } var parameters = new List <IParameterDefinition>(accessor.Parameters); if (accessor == setter) // If setter remove value parameter. { parameters.RemoveAt(parameters.Count - 1); } WriteParameters(parameters, property.ContainingType, true); } else { WriteIdentifier(property.Name); } WriteSpace(); WriteSymbol("{"); //get if (getter != null) { WriteAccessorDefinition(property, getter, "get"); } //set if (setter != null) { WriteAccessorDefinition(property, setter, "set"); } WriteSpace(); WriteSymbol("}"); }