Testura/Testura.Code

Attribute properties

Closed this issue · 3 comments

Is it possible to generate something like:

MyAttribute("somevalue", MyProperty = "something")

I solved it creating my own argument and using Roslyn explicitly.

class PropertyInitializationArgument : Argument
    {
        public string PropertyName { get; set; }
        public object Value { get; set; }

        public PropertyInitializationArgument(string propertyName, object value) : base(null)
        {
            PropertyName = propertyName;
            Value = value;
        }

        protected override ArgumentSyntax CreateArgumentSyntax()
        {
            var value = Value.ToString();
            if (Value is bool)
            {
                value = value.ToLower();
            }

            return SyntaxFactory.Argument(SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.IdentifierName(PropertyName), SyntaxFactory.IdentifierName(value)));
        }
    }

That's actually really nice to have. Could you maybe create a new PR with that class and a small unit test? Or is it okay if I add it myself?

Feel free to add it yourself. I just needed something quick and it works for me.