Reputation: 31
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?
Upvotes: 0
Views: 2320
Reputation: 6778
CodeAttributeArgument codeAttr = new CodeAttributeArgument(new CodePrimitiveExpression("Max Branches = 1000"));
CodeAttributeDeclaration codeAttrDecl = new CodeAttributeDeclaration("PexMethod",codeAttr);
mymethod.CustomAttributes.Add(codeAttrDecl);
Upvotes: 0
Reputation: 244767
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)
}
}
}
}
};
Upvotes: 2
Reputation: 27913
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.
Upvotes: 0
Reputation: 8872
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.
Upvotes: 2