0

I am using CodeDom to generate a class which include some methods. I was able to declare an attribute for my methods to look similar as what Pex does when it creates a parameterized unit test:

[PexMethod]
public void myMethod()

However I would like to include something more to it like:

[PexMethod (Max Branches = 1000)]
public void myMethod()

But I am not able to include the ((Max Branches = 1000)). Could you somebody help me a bit?

svick
  • 236,525
  • 50
  • 385
  • 514
Peter
  • 33
  • 1
  • 6

4 Answers4

2

You can't have spaces in the attribute values, they are just wrappers around public properties in your custom attributes class. For example:

public class TestAttribute : Attribute
{
    public bool Enabled { get; set; }
}

And you can use this like this

[TestAttribute(Enabled = true)]
void Foo(){}

So since the attribute maps to a property it has to follow normal syntactical naming rules.

devshorts
  • 8,572
  • 4
  • 50
  • 73
  • My bad, in fact the attribute should be without spaces [PexMethod(MaxBranches=1000)], but even so, how you do it with codeDom? – Peter Apr 30 '12 at 21:36
  • Sorry Peter, can't help ya there. I've never used codeDom, but if the attribute values have a public setter and it inherits from the attribute class you should be able to do this. – devshorts Apr 30 '12 at 21:41
  • Thanks anyway for the feedback :) – Peter May 01 '12 at 12:54
2

I'm not sure what your problem is, but you can simply set the Value property on CodeAttributeArgument:

var method =
    new CodeMemberMethod
    {
        Name = "MyMethod",
        CustomAttributes =
        {
            new CodeAttributeDeclaration
            {
                Name = "PexMethod",
                Arguments =
                {
                    new CodeAttributeArgument
                    {
                        Name = "MaxBranches",
                        Value = new CodePrimitiveExpression(1000)
                    }
                }
            }
        }
    };
svick
  • 236,525
  • 50
  • 385
  • 514
  • I know this is a long shot, seeing as this thread is 2 years old, but how do I create arguments with enum values? [WebInvoke(Method = "POST", UriTemplate = "MyTemplate", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] I've managed to do the strings, but I don't know what to do about the enums - RequestFormat = WebMessageFormat.Json and ResponseFormat = WebMessageFormat.Json – Shaggydog Aug 07 '14 at 15:55
  • Also, how would I go about creating this [FaultContract(typeof(Collection))] – Shaggydog Aug 07 '14 at 16:13
  • @Shaggydog You should ask a new question about that. – svick Aug 07 '14 at 18:07
0

The MaxBranches property is on a base class (PexSettingsAttributeBase). That may be why you are having trouble. You may be reflecting over the wrong type to find the PropertyInfo to set.

agent-j
  • 27,335
  • 5
  • 52
  • 79
0
    CodeAttributeArgument codeAttr = new CodeAttributeArgument(new CodePrimitiveExpression("Max Branches = 1000"));
     CodeAttributeDeclaration codeAttrDecl = new CodeAttributeDeclaration("PexMethod",codeAttr);

 mymethod.CustomAttributes.Add(codeAttrDecl);
Lijo
  • 6,498
  • 5
  • 49
  • 60