Paul Coetser
Paul Coetser

Reputation: 31

How to change attributes with Roslyn

I struggle to find the correct methods on the roslyn library to make changes to the Attributes and rewrite the attribute:

class Program
{ 
    static void Main(string[] args)
    {

        //this is the original class 
        var str = @"public class OfferModel
        {
            public int Id { get; set; }

            [Required(ErrorMessage = \""Required\"")]
            [Display(Name = \""Date of Offer *\"")]
            public DateTime? OfferDate { get; set; }

            [Required(ErrorMessage = \""Required\"")]
            [Display(Name = \""Amount *\"")]
            [Range(1, double.MaxValue, ErrorMessage = \""Invalid Amount\"")]
            public decimal? Amount { get; set; }
        }";

        var tree = CSharpSyntaxTree.ParseText(str);

        var myWriter = new MyReWriter();
        var newRoot = myWriter.Visit(tree.GetRoot());

        Console.WriteLine(newRoot.ToFullString());

    }
}

public class MyReWriter : CSharpSyntaxRewriter
{
    public MyReWriter() : base() { }

    public override SyntaxNode VisitAttribute(AttributeSyntax node)
    {
        var newNode = node;

        if (node.Name.ToString() == "Required")
        {
            newNode = PrepareNewRequiredArgument(node);
        }

        return base.VisitAttribute(newNode);
    }

    private AttributeSyntax PrepareNewRequiredArgument(AttributeSyntax node)
    {
        var newNode = node;
        //ErrorMessageResourceType
        //ErrorMessageResourceName
        var argResType = node.ArgumentList.Arguments.FirstOrDefault(aa => aa.NameEquals.Name.Identifier.Text == "ErrorMessageResourceType");
        var argResName = node.ArgumentList.Arguments.FirstOrDefault(aa => aa.NameEquals.Name.Identifier.Text == "ErrorMessageResourceName");
        if (argResType != null && argResName != null)
        {
            //already exists, don't do anything
            return newNode;
        }

        var argErrorMessage = node.ArgumentList.Arguments.FirstOrDefault(aa => aa.NameEquals.Name.Identifier.Text == "ErrorMessage");

        if (argErrorMessage != null)
        {
            var name = ParseName("Required");
            var tokenValue = ((LiteralExpressionSyntax)argErrorMessage.Expression).Token.Value;

            var argErrorMessageResourceName = AttributeArgument(
                LiteralExpression(SyntaxKind.StringLiteralExpression,
                    Token(default(SyntaxTriviaList),
                            SyntaxKind.StringLiteralToken, "ErrorMessageResourceName", tokenValue.ToString(), default(SyntaxTriviaList))));

            //var argErrorMessageResourceType = AttributeArgument(
            //   LiteralExpression(SyntaxKind.NumericLiteralExpression, 
            //        Token(default(SyntaxTriviaList),
            //                SyntaxKind.NumericLiteralToken, "ErrorMessageResourceType", "typeof(WebModels)", default(SyntaxTriviaList))));

            var otherList = new SeparatedSyntaxList<AttributeArgumentSyntax>();
            otherList = otherList.AddRange(new[] { argErrorMessageResourceName });
            //otherList = otherList.AddRange(new[] { argErrorMessageResourceName, argErrorMessageResourceType });
            var argumentList = AttributeArgumentList(otherList);
            var newAttribute = Attribute(name, argumentList);

            newNode = node.ReplaceNode(node, newAttribute);
        }
        return newNode;
    }
}

The output I require must look like follow:

public class OfferModel
{
    public int Id { get; set; }

    [Required(ErrorMessageResourceType = typeof(WebModels), ErrorMessageResourceName = \""Required\"")]
    [Display(Name = \""Date of Offer *\"")]
    public DateTime? OfferDate { get; set; }

    [Required(ErrorMessageResourceType = typeof(WebModels), ErrorMessageResourceName = "Required")]
    [Display(ResourceType = typeof(WebModels), Name = "Amount *")]
    [Range(1, double.MaxValue, ErrorMessageResourceName = "InvalidAmount")]
    public decimal? Amount { get; set; }
}

The same will apply for the [Display] / [Range] attributes. If I can get the [Required] to work, I can apply the same to [Display] / [Range]

Upvotes: 3

Views: 1327

Answers (1)

Matt Warren
Matt Warren

Reputation: 2016

Your last line is:

newNode = node.ReplaceNode(node, newAttribute);

it seems you are just trying to replace the root of a subtree (node) with a different instance (newAttribute). ReplaceNode won't work on the root node. Instead, you should just use the new node.

it should just be:

newNode = newAttribute;

Upvotes: 1

Related Questions