2016-08-05 4 views
0

を使用して、テストメソッド内のカテゴリ属性を読みます:は、私は、リフレクションを使用して.dllファイルを読み込むことで<code>TestMethod</code>カスタム属性のカテゴリプロパティを使用して、このような何かをテストメソッドを追加しようとしているリフレクション

[TestMethod, Category("xyz"), Category("abc")] 
public void VerifySomething() 
{ 
    //some code 
} 

私はこのような反射を利用した.dllを読んでいる:

Assembly _assembly = Assembly.LoadFile(AssembleyPath); 
      Type[] types = _assembly.GetExportedTypes(); 
      foreach (Type type in types) 
      { 
       Attribute _classAttribute = type.GetCustomAttribute(typeof(TestClassAttribute)); 
       if (_classAttribute != null) 
       { 
        TreeNode _n = new TreeNode(); 
        _n.Text = type.Name; 
        if (type.Name.ToLower().Contains("testbase")) 
         // For exculding test base class from tree node list 
         continue; 
        MemberInfo[] members = 
         type.GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod); 
        foreach (MemberInfo member in members) 
        { 

          Attribute _att = member.GetCustomAttribute(typeof(TestMethodAttribute)); 
          if (_att != null) 
          { 
           TestMethod _t = new TestMethod(member.Name); 

          } 

        } 
       } 
      } 

私は特にによってフィルタリングテストメソッドを追加することができないよ、これを使用しますツリービューノード内のカテゴリ。 基本的には、私が望むのは、上記の例のように "xyz"や "abc"のような値を持つdllからTestCategoryAttributeを読み込み、フィルタ(TestCategoryAttribute)の値に基づいてメソッドをロードすることですdllを作成し、treeviewノードに追加します。誰も私を導くことができますか私はそれを達成することができます。

+0

私は、すべてのテストメソッドを取得していますが、私はにできないよ、私はTestMethodsをフィルタリングすることがありますかに基づいてTestCategoryAttributeを読み、DLLの読み込み中にツリーノード –

+0

にそれらを追加することができませんよテストカテゴリ –

+0

が正しく入力されています。そのヌルではありません –

答えて

0

TestMethodMethodInfoとして取得する方法は、Reflectionです。 TestMethod_t考える

var _t= typeof(testClass).GetMethod(methodName); // testClass should be the type in your foreach 

(と仮定すると、すべてがすぐそこまである)、あなたはいくつかの条件に基づいてそれらをフィルタリングする方法次の

var testCategories = IEnumerable<TestCategoryAttribute>)_t.GetCustomAttributes(typeof(TestCategoryAttribute), true)); 

でカテゴリを得ることができますか? Linq Anyはこのトリックを行いますか?

if testCategories.Any(c => c == "xyz" || c == "abc")) 
    { 
     // then add to the treeview node, etc... 
+0

このステップの後に行を挿入した後、_tでこのエラーが発生する - TestMethod _t = new TestMethod(member.Name); string key = string.Format( "K {0}"、TotalCountOfTests ++); var testCategories =(IEnumerable )_ t.GetCustomAttributes(typeof(TestCategoryAttribute)、true); エラー\t CS1929 \t「のtestMethodは、」「GetCustomAttributes」と最高の拡張メソッドのオーバーロード「CustomAttributeExtensions.GetCustomAttributes(のMemberInfo、タイプ、ブール値)」の定義が含まれていません「のMemberInfo」:( –

+0

は、あなたが見たことがありのタイプの受信機が必要ですこの部分: 'var _t = typeof(testClass).GetMethod(methodName);'?testClassはforeach内の型にする必要がありますが、まだmethodNameがあるかどうかはわかりません... –

+0

私はすでにメソッドを取得していますここで名前をつけてください - TestMethod _t = new TestMethod(member.Name); –

関連する問題