简体   繁体   English

ExcludeFromCodeCoverage排除自动生成的代码

[英]ExcludeFromCodeCoverage Exclude Auto-Generated Code

Is there a way to mark an auto-generated class as ExcludeFromCodeCoverage. 有没有办法将自动生成的类标记为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. 但是如果你打开自动生成的man的代码并将类标记为ExcludeFromCodeCoverage,那么一旦你重新生成该类,它就会被覆盖。

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. 我可以在dbml后面的代码中创建部分类,并将该属性应用于它,然而,它可以用于很多部分类。

You can use PostSharp or other AOP framework to create aspect which will apply ExcludeFromCodeCoverageAttribute to specified types or namespaces: 您可以使用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)};
    }
}

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: 在编译期间,PostSharp会将ExcludeFromCodeCoverageAttribute添加到My.AutogeneratedCode命名空间中的所有类:

[assembly: DisableCoverage(AttributeTargetTypes="My.AutogeneratedCode.*")]

Sample code and explanations you can find here . 您可以在此处找到示例代码和说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM