4

I am trying to create a unit test that will only fail for types which don't suppress the corresponding message. However, I am unable to access the SuppressMessage attributes on any of my types in my unit tests. Is it possible to access SuppressMessage attributes at runtime? I've included a simplified version of my unit test.

[System.Diagnostics.CodeAnalysis.SuppressMessage("Foo", "Bar")]
public interface IMyInterface { }

public void UnitTest()
{
    var getCustomAttributes = typeof(IMyInterface).GetCustomAttributes();   //Returns an empty array
    //Skip check if message should be suppressed
}
Steven Wexler
  • 16,589
  • 8
  • 53
  • 80

1 Answers1

2

Build your assembly (in which the IMyInterface is defined) with conditional symbol CODE_ANALYSIS

[Conditional("CODE_ANALYSIS")]
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
public sealed class SuppressMessageAttribute : Attribute {
Ondrej Svejdar
  • 21,349
  • 5
  • 54
  • 89