private string GetNameObjectPart( IEventSymbol eventSymbol, SyntaxToken plusEqualsToken, SemanticModel semanticModel, ISyntaxFactsService syntaxFactsService ) { AssertIsBackground(); var parentToken = plusEqualsToken.Parent as AssignmentExpressionSyntax; if (parentToken.Left is MemberAccessExpressionSyntax memberAccessExpression) { // This is expected -- it means the last thing is(probably) the event name. We // already have that in eventSymbol. What we need is the LHS of that dot. var lhs = memberAccessExpression.Expression; if (lhs is MemberAccessExpressionSyntax lhsMemberAccessExpression) { // Okay, cool. The name we're after is in the RHS of this dot. return(lhsMemberAccessExpression.Name.ToString()); } if (lhs is NameSyntax lhsNameSyntax) { // Even easier -- the LHS of the dot is the name itself return(lhsNameSyntax.ToString()); } } // If we didn't find an object name above, then the object name is the name of this class. // Note: For generic, it's ok(it's even a good idea) to exclude type variables, // because the name is only used as a prefix for the method name. var typeDeclaration = syntaxFactsService.GetContainingTypeDeclaration( semanticModel.SyntaxTree.GetRoot(), plusEqualsToken.SpanStart ) as BaseTypeDeclarationSyntax; return(typeDeclaration != null ? typeDeclaration.Identifier.Text : eventSymbol.ContainingType.Name); }
/// <summary> /// Take another look at the LHS of the += node -- we need to figure out a default name /// for the event handler, and that's usually based on the object (which is usually a /// field of 'this', but not always) to which the event belongs. So, if the event is /// something like 'button1.Click' or 'this.listBox1.Select', we want the names /// 'button1' and 'listBox1' respectively. If the field belongs to 'this', then we use /// the name of this class, as we do if we can't make any sense out of the parse tree. /// </summary> private string GetNameObjectPart(IEventSymbol eventSymbol, SyntaxToken plusEqualsToken, SemanticModel semanticModel, ISyntaxFactsService syntaxFactsService) { AssertIsBackground(); var parentToken = plusEqualsToken.Parent as AssignmentExpressionSyntax; var memberAccessExpression = parentToken.Left as MemberAccessExpressionSyntax; if (memberAccessExpression != null) { // This is expected -- it means the last thing is(probably) the event name. We // already have that in eventSymbol. What we need is the LHS of that dot. var lhs = memberAccessExpression.Expression; var lhsMemberAccessExpression = lhs as MemberAccessExpressionSyntax; if (lhsMemberAccessExpression != null) { // Okay, cool. The name we're after is in the RHS of this dot. return lhsMemberAccessExpression.Name.ToString(); } var lhsNameSyntax = lhs as NameSyntax; if (lhsNameSyntax != null) { // Even easier -- the LHS of the dot is the name itself return lhsNameSyntax.ToString(); } } // If we didn't find an object name above, then the object name is the name of this class. // Note: For generic, it's ok(it's even a good idea) to exclude type variables, // because the name is only used as a prefix for the method name. var typeDeclaration = syntaxFactsService.GetContainingTypeDeclaration( semanticModel.SyntaxTree.GetRoot(), plusEqualsToken.SpanStart) as BaseTypeDeclarationSyntax; return typeDeclaration != null ? typeDeclaration.Identifier.Text : eventSymbol.ContainingType.Name; }