您可以使用PostSharp或其他AOP框架創建方面將適用ExcludeFromCodeCoverageAttribute
指定類型或命名空間:
[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將在My.AutogeneratedCode
命名空間添加ExcludeFromCodeCoverageAttribute
的所有類:
[assembly: DisableCoverage(AttributeTargetTypes="My.AutogeneratedCode.*")]
示例代碼和解釋,你可以找到here。
你可以讓自動生成的類帶有「partial」(比如,以某種方式改變生成器)嗎? –