0

CodeDom を使用して、いくつかのメソッドを含むクラスを生成しています。パラメーター化された単体テストを作成するときに Pex が行うのと同じように、メソッドの属性を宣言することができました。

[PexMethod]
public void myMethod()

ただし、次のようなものをさらに含めたいと思います。

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

しかし、私は含めることができません((Max Branches = 1000))。誰か少し手伝ってくれませんか?

4

4 に答える 4

2

問題が何であるかはわかりませんが、プロパティを次のように設定するValueだけですCodeAttributeArgument

var method =
    new CodeMemberMethod
    {
        Name = "MyMethod",
        CustomAttributes =
        {
            new CodeAttributeDeclaration
            {
                Name = "PexMethod",
                Arguments =
                {
                    new CodeAttributeArgument
                    {
                        Name = "MaxBranches",
                        Value = new CodePrimitiveExpression(1000)
                    }
                }
            }
        }
    };
于 2012-04-30T21:39:05.400 に答える
2

属性値にスペースを含めることはできません。スペースは、カスタム属性クラスのパブリック プロパティの単なるラッパーです。例えば:

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

そして、あなたはこれをこのように使うことができます

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

したがって、属性はプロパティにマップされるため、通常の構文命名規則に従う必要があります。

于 2012-04-30T21:13:10.860 に答える
0
    CodeAttributeArgument codeAttr = new CodeAttributeArgument(new CodePrimitiveExpression("Max Branches = 1000"));
     CodeAttributeDeclaration codeAttrDecl = new CodeAttributeDeclaration("PexMethod",codeAttr);

 mymethod.CustomAttributes.Add(codeAttrDecl);
于 2016-07-21T13:13:25.253 に答える
0

MaxBranchesプロパティは基本クラス ( PexSettingsAttributeBase ) にあります。それがあなたが困っている理由かもしれません。設定する PropertyInfo を見つけるために間違った型を検討している可能性があります。

于 2012-04-30T21:16:15.660 に答える