我最近将.NET 3.5的C#项目升级到.NET 4.我有一个方法从给定的MethodBase
实例列表中提取所有MSTest测试方法。它的机身看起来是这样的:.NET中的自定义属性更改4
return null == methods || methods.Count() == 0
? null
: from method in methods
let testAttribute = Attribute.GetCustomAttribute(method,
typeof(TestMethodAttribute))
where null != testAttribute
select method;
这个工作在.NET 3.5,但因为我的升级项目.NET 4中,这个代码总是返回一个空列表,即使给出的方法包含一个方法列表标记为[TestMethod]
。 .NET 4中的自定义属性有所改变吗?
调试,我发现GetCustomAttributesData()
上的测试方法,结果给出了两个CustomAttributeData
一个列表,它是在Visual Studio 2010中的“当地人”窗口描述:
Microsoft.VisualStudio.TestTools.UnitTesting.DeploymentItemAttribute("myDLL.dll")
Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute()
- 这就是我正在寻找
当我打电话GetType()
对第二CustomAttributeData
实例,但是,我得到{Name = "CustomAttributeData" FullName = "System.Reflection.CustomAttributeData"} System.Type {System.RuntimeType}
。如何从CustomAttributeData
中获得TestMethodAttribute
,以便我可以从MethodBase
列表中提取测试方法?
是的,我试过传递'true',所以它也检查祖先。我总是找回一个空的“对象”数组。 – 2010-06-01 17:51:12
@Sarah:在这种情况下,请发布一个简短但完整的程序来展示问题。我已经展示了一个* does *工作的例子。 – 2010-06-01 17:52:09
虚惊一场!我的项目引用了旧版.NET 3.5/VS 2008版本的UnitTestFramework库。切换到.NET 4/VS 2010版本的UnitTestFramework(10.0.0.0)解决了这个问题。 – 2010-06-01 18:08:56