private IMethodSymbol GenerateSetAccessor( Compilation compilation, IPropertySymbol property, Accessibility accessibility, bool generateAbstractly, bool useExplicitInterfaceSymbol, ImplementTypePropertyGenerationBehavior propertyGenerationBehavior, INamedTypeSymbol[] attributesToRemove, CancellationToken cancellationToken) { if (property.SetMethod == null) { return(null); } if (property.GetMethod == null) { // Can't have an auto-prop with just a setter. propertyGenerationBehavior = ImplementTypePropertyGenerationBehavior.PreferThrowingProperties; } var setMethod = property.SetMethod.RemoveInaccessibleAttributesAndAttributesOfTypes( this.State.ClassOrStructType, attributesToRemove); return(CodeGenerationSymbolFactory.CreateAccessorSymbol( setMethod, attributes: default(ImmutableArray <AttributeData>), accessibility: accessibility, explicitInterfaceSymbol: useExplicitInterfaceSymbol ? property.SetMethod : null, statements: GetSetAccessorStatements( compilation, property, generateAbstractly, propertyGenerationBehavior, cancellationToken))); }
private ISymbol GenerateProperty( Compilation compilation, IPropertySymbol property, Accessibility accessibility, DeclarationModifiers modifiers, bool generateAbstractly, bool useExplicitInterfaceSymbol, string memberName, ImplementTypePropertyGenerationBehavior propertyGenerationBehavior ) { var factory = Document.GetLanguageService <SyntaxGenerator>(); var attributesToRemove = AttributesToRemove(compilation); var getAccessor = GenerateGetAccessor( compilation, property, accessibility, generateAbstractly, useExplicitInterfaceSymbol, propertyGenerationBehavior, attributesToRemove ); var setAccessor = GenerateSetAccessor( compilation, property, accessibility, generateAbstractly, useExplicitInterfaceSymbol, propertyGenerationBehavior, attributesToRemove ); var syntaxFacts = Document.Project.LanguageServices.GetRequiredService <ISyntaxFactsService>(); var parameterNames = NameGenerator.EnsureUniqueness( property.Parameters.SelectAsArray(p => p.Name), isCaseSensitive: syntaxFacts.IsCaseSensitive ); var updatedProperty = property.RenameParameters(parameterNames); updatedProperty = updatedProperty.RemoveInaccessibleAttributesAndAttributesOfTypes( compilation.Assembly, attributesToRemove ); return(CodeGenerationSymbolFactory.CreatePropertySymbol( updatedProperty, accessibility: accessibility, modifiers: modifiers, explicitInterfaceImplementations: useExplicitInterfaceSymbol ? ImmutableArray.Create(property) : default,
private ISymbol GenerateMember( ISymbol member, ImplementTypePropertyGenerationBehavior propertyGenerationBehavior, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); // Check if we need to add 'unsafe' to the signature we're generating. var syntaxFacts = _document.GetLanguageService <ISyntaxFactsService>(); var addUnsafe = member.IsUnsafe() && !syntaxFacts.IsUnsafeContext(_state.Location); return(GenerateMember(member, addUnsafe, propertyGenerationBehavior, cancellationToken)); }
private ISymbol GenerateProperty( Compilation compilation, IPropertySymbol property, Accessibility accessibility, DeclarationModifiers modifiers, bool generateAbstractly, bool useExplicitInterfaceSymbol, string memberName, ImplementTypePropertyGenerationBehavior propertyGenerationBehavior, CancellationToken cancellationToken) { var factory = this.Document.GetLanguageService<SyntaxGenerator>(); var attributesToRemove = AttributesToRemove(compilation);
private ISymbol GenerateMember( ISymbol member, bool addUnsafe, ImplementTypePropertyGenerationBehavior propertyGenerationBehavior, CancellationToken cancellationToken) { var modifiers = new DeclarationModifiers(isOverride: true, isUnsafe: addUnsafe); var accessibility = member.ComputeResultantAccessibility(_state.ClassType); return(member switch { IMethodSymbol method => GenerateMethod(method, modifiers, accessibility, cancellationToken), IPropertySymbol property => GenerateProperty(property, modifiers, accessibility, propertyGenerationBehavior), IEventSymbol @event => CodeGenerationSymbolFactory.CreateEventSymbol( @event, accessibility: accessibility, modifiers: modifiers), _ => null, });
private ImmutableArray <SyntaxNode> GetSetAccessorStatements( Compilation compilation, IPropertySymbol property, bool generateAbstractly, ImplementTypePropertyGenerationBehavior propertyGenerationBehavior, CancellationToken cancellationToken) { if (generateAbstractly) { return(default(ImmutableArray <SyntaxNode>)); } var factory = this.Document.GetLanguageService <SyntaxGenerator>(); if (ThroughMember != null) { var throughExpression = CreateThroughExpression(factory); SyntaxNode expression; if (property.IsIndexer) { expression = throughExpression; } else { expression = factory.MemberAccessExpression( throughExpression, factory.IdentifierName(property.Name)); } if (property.Parameters.Length > 0) { var arguments = factory.CreateArguments(property.Parameters.As <IParameterSymbol>()); expression = factory.ElementAccessExpression(expression, arguments); } expression = factory.AssignmentStatement(expression, factory.IdentifierName("value")); return(ImmutableArray.Create(factory.ExpressionStatement(expression))); } return(propertyGenerationBehavior == ImplementTypePropertyGenerationBehavior.PreferAutoProperties ? default(ImmutableArray <SyntaxNode>) : factory.CreateThrowNotImplementedStatementBlock(compilation)); }
private IPropertySymbol GenerateProperty( IPropertySymbol property, DeclarationModifiers modifiers, Accessibility accessibility, ImplementTypePropertyGenerationBehavior propertyGenerationBehavior, CancellationToken cancellationToken) { if (property.GetMethod == null) { // Can't generate an auto-prop for a setter-only property. propertyGenerationBehavior = ImplementTypePropertyGenerationBehavior.PreferThrowingProperties; } var syntaxFactory = _document.GetLanguageService <SyntaxGenerator>(); var accessorBody = propertyGenerationBehavior == ImplementTypePropertyGenerationBehavior.PreferAutoProperties ? default : syntaxFactory.CreateThrowNotImplementedStatementBlock(_model.Compilation); var getMethod = ShouldGenerateAccessor(property.GetMethod) ? CodeGenerationSymbolFactory.CreateAccessorSymbol( property.GetMethod, attributes: default, accessibility: property.GetMethod.ComputeResultantAccessibility(_state.ClassType), statements: accessorBody) : null; var setMethod = ShouldGenerateAccessor(property.SetMethod) ? CodeGenerationSymbolFactory.CreateAccessorSymbol( property.SetMethod, attributes: default, accessibility: property.SetMethod.ComputeResultantAccessibility(_state.ClassType), statements: accessorBody) : null; return(CodeGenerationSymbolFactory.CreatePropertySymbol( property, accessibility: accessibility, modifiers: modifiers, getMethod: getMethod, setMethod: setMethod)); }
private ISymbol GenerateProperty( Compilation compilation, IPropertySymbol property, Accessibility accessibility, DeclarationModifiers modifiers, bool generateAbstractly, bool useExplicitInterfaceSymbol, string memberName, ImplementTypePropertyGenerationBehavior propertyGenerationBehavior, CancellationToken cancellationToken) { var factory = this.Document.GetLanguageService <SyntaxGenerator>(); var attributesToRemove = AttributesToRemove(compilation); var getAccessor = GenerateGetAccessor( compilation, property, accessibility, generateAbstractly, useExplicitInterfaceSymbol, propertyGenerationBehavior, attributesToRemove, cancellationToken); var setAccessor = GenerateSetAccessor( compilation, property, accessibility, generateAbstractly, useExplicitInterfaceSymbol, propertyGenerationBehavior, attributesToRemove, cancellationToken); var syntaxFacts = Document.GetLanguageService <ISyntaxFactsService>(); var parameterNames = NameGenerator.EnsureUniqueness( property.Parameters.Select(p => p.Name).ToList(), isCaseSensitive: syntaxFacts.IsCaseSensitive); var updatedProperty = property.RenameParameters(parameterNames); updatedProperty = updatedProperty.RemoveAttributeFromParameters(attributesToRemove); // TODO(cyrusn): Delegate through throughMember if it's non-null. return(CodeGenerationSymbolFactory.CreatePropertySymbol( updatedProperty, accessibility: accessibility, modifiers: modifiers, explicitInterfaceSymbol: useExplicitInterfaceSymbol ? property : null, name: memberName, getMethod: getAccessor, setMethod: setAccessor)); }