/// <summary> /// Add the attribute list to the <see cref="DelegateDeclarationSyntax"/>. /// </summary> /// <param name="member">The <see cref="DelegateDeclarationSyntax"/>.</param> /// <param name="attributeList">The <see cref="AttributeListSyntax"/>.</param> /// <returns>The <paramref name="member"/> with <paramref name="attributeList"/> added.</returns> public static DelegateDeclarationSyntax WithAttributeList(this DelegateDeclarationSyntax member, AttributeListSyntax attributeList) { if (member is null) { throw new System.ArgumentNullException(nameof(member)); } if (attributeList is null) { throw new System.ArgumentNullException(nameof(attributeList)); } return(member.WithAttributeLists(member.AttributeLists.Add(attributeList))); }
public override SyntaxNode VisitDelegateDeclaration(DelegateDeclarationSyntax node) { string fullName = SyntaxUtils.GetFullName(node); // Remove duplicate delegates in this tree if (this.visitedDelegateNames.Contains(fullName)) { return(null); } this.visitedDelegateNames.Add(fullName); string returnFullName = $"{fullName}::return"; if (this.GetRemapInfo(returnFullName, out List <AttributeSyntax> listAttributes, node.ReturnType.ToString(), out var newType, out _)) { node = (DelegateDeclarationSyntax)base.VisitDelegateDeclaration(node); if (listAttributes != null) { foreach (var attrNode in listAttributes) { var attrListNode = SyntaxFactory.AttributeList( SyntaxFactory.SingletonSeparatedList <AttributeSyntax>(attrNode)) .WithTarget( SyntaxFactory.AttributeTargetSpecifier( SyntaxFactory.Token(SyntaxKind.ReturnKeyword))); node = node.WithAttributeLists(node.AttributeLists.Add(attrListNode)); } if (newType != null) { node = node.WithReturnType(SyntaxFactory.ParseTypeName(newType).WithTrailingTrivia(SyntaxFactory.Space)); } } return(node); } return(base.VisitDelegateDeclaration(node)); }
/// <summary> /// 构建委托 /// </summary> /// <returns></returns> public DelegateDeclarationSyntax BuildSyntax() { DelegateDeclarationSyntax memberDeclaration = default; var syntaxNodes = CSharpSyntaxTree.ParseText(ToFullCode()) .GetRoot() .DescendantNodes(); memberDeclaration = syntaxNodes .OfType <DelegateDeclarationSyntax>().FirstOrDefault(); if (memberDeclaration is null) { throw new InvalidOperationException("请检查代码语法是否有错误!"); } // 添加特性 if (_member.Atributes.Count != 0) { var tmp = CodeSyntax.CreateAttributeList(_member.Atributes.ToArray()); memberDeclaration = memberDeclaration.WithAttributeLists(tmp); } return(memberDeclaration); }