ExcludeFromCodeCoverage 排除自动生成的代码 [英] ExcludeFromCodeCoverage Exclude Auto-Generated Code
问题描述
有没有办法将自动生成的类标记为 ExcludeFromCodeCoverage.我正在其他领域使用该属性并且效果很好.但是如果你打开自动生成的家伙的代码并将这些类标记为 ExcludeFromCodeCoverage,一旦你重新生成那个类,它就会被覆盖.
Is there a way to mark an auto-generated class as ExcludeFromCodeCoverage. I am using that attribute in other areas and works great. But if you open the code of the auto-generated guy and mark the classes as ExcludeFromCodeCoverage, once you re-generate that class itll be over written.
我可以在 dbml 后面的代码中创建分部类,并将该属性应用到它,它可以工作,但是,这会产生很多分部类.
I can create partial classes in the code behind of the dbml and apply that attribute to it and it works, however, that would make for a lot of partial classes.
推荐答案
您可以使用 PostSharp或其他 AOP 框架来创建方面,将 ExcludeFromCodeCoverageAttribute
应用于指定的类型或命名空间:
You can use PostSharp or other AOP framework to create aspect which will apply ExcludeFromCodeCoverageAttribute
to specified types or namespaces:
[Serializable]
[AttributeUsage(AttributeTargets.Assembly)]
[MulticastAttributeUsage(MulticastTargets.Class | MulticastTargets.Struct)]
[ProvideAspectRole(StandardRoles.PerformanceInstrumentation)]
public sealed class DisableCoverageAttribute : TypeLevelAspect, IAspectProvider
{
public IEnumerable<AspectInstance> ProvideAspects(object targetElement)
{
Type disabledType = (Type)targetElement;
var introducedExclusion = new CustomAttributeIntroductionAspect(
new ObjectConstruction(typeof (ExcludeFromCodeCoverageAttribute)));
return new[] {new AspectInstance(disabledType, introducedExclusion)};
}
}
然后将此方面应用于程序集并提供要排除的命名空间.在编译期间,PostSharp 会将 ExcludeFromCodeCoverageAttribute
添加到 My.AutogeneratedCode
命名空间中的所有类:
Then just apply this aspect to assembly and provide namespace which you want to exclude. During compilation PostSharp will add ExcludeFromCodeCoverageAttribute
to all classes in My.AutogeneratedCode
namespace:
[assembly: DisableCoverage(AttributeTargetTypes="My.AutogeneratedCode.*")]
您可以在此处找到示例代码和说明.
Sample code and explanations you can find here.
这篇关于ExcludeFromCodeCoverage 排除自动生成的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!