Corresponding CLS attributes and array parameters

I created an attribute that accepts an array (params) in its constructor.

internal class MyTestAttribute : Attribute
{
    public MyTestAttribute (params Options[] options)
    {
        ....
    }
}

Option here is an enumeration (with many values), so an example of a request site would be

[MyTest(Option.One, Option.Three)]
internal void SomeMethod(int param1, long param2)
{
  ....
}

Everything is still smooth and the setup works, but I get a warning “Arrays as attributes is not CLS-compliant” on every client site. Now I have to admit that I do not need to use this assembly anywhere except C #, and I do not warn about errors, but hundreds of warnings become annoying.

The obvious solution is to disable CLS compliance, but at the moment I cannot do this.

Is there a different approach to creating an attribute that will still do the same thing, but get rid of the warnings?

+5
2

:

1: :

private MyTestAttribute(Option[] options) {...}
public MyTestAttribute(Option option0)
          : this(new[] {option0}) {}
public MyTestAttribute(Option option0, Option option1)
          : this(new[] {option0, option1}) {}
public MyTestAttribute(Option option0, Option option1, Option option2)
          : this(new[] {option0, option1, option2}) {}
// add a few more, without going crazy

2: Options , [Flags] :

[MyTest(Option.One | Option.Two)]

:

[Flags]
public enum Option {
     None = 0,
     One = 1,
     Two = 2,
     Three = 4,
     Four = 8,
     ...
}
+11

" CLS"

, .

, :

internal class MyTestAttribute : Attribute
{
    public MyTestAttribute(Option o1) : this(new Option[] { o1 }) {}
    public MyTestAttribute(Option o1, Option o2) : 
      this(new Option[] { o1, o2 }) {}

    MyTestAttribute (Options[] options)
    {
        ....
    }
}

. , CLS , . AFAIK, CLS (public/protected).

+4

All Articles