I have created an attribute that accepts a (params) array in its constructor.
internal class MyTestAttribute : Attribute
{
public MyTestAttribute (params Options[] options)
{
....
}
}
Option
here is an enum (with lots of values), so a sample call site will be
[MyTest(Option.One, Option.Three)]
internal void SomeMethod(int param1, long param2)
{
....
}
Everything is peachy so far, and the setup works, but I'm receiving an "Arrays as attribute arguments is not CLS-compliant" warning on each call-site. Now, I have to admit that I do not need to use this assembly from anywhere other that C#, nor do I do warnings-as-errors, but the hundreds of warnings are getting annoying.
The obvious solution is to turn off CLS-compliance, but at the moment I can't do that.
Is there some other approach to the creation of the attribute that will still do the same thing, but get rid of the warnings?
Being CLS compliant means that you can write code that can be consumed by any language that can be compiled and run on the CLR. But CLS compliance is not required, giving you the flexibility in cases where CLS compliance would be hard or impossible to do.
A CLS-compliant identifier should not start with an underscore. Show activity on this post. It's the underscore.
Two options:
1: you could add some overloads instead:
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: if Options
is an enum, mark the enum as a [Flags]
enum, and have the caller combine them:
[MyTest(Option.One | Option.Two)]
with:
[Flags]
public enum Option {
None = 0,
One = 1,
Two = 2,
Three = 4,
Four = 8,
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With