Can I initialize a C# attribute with an array or other variable number of arguments?

后端 未结 7 2187
生来不讨喜
生来不讨喜 2020-11-28 05:56

Is it possible to create an attribute that can be initialized with a variable number of arguments?

For example:

[MyCustomAttribute(new int[3,4,5])]           


        
7条回答
  •  情歌与酒
    2020-11-28 06:41

    You can do that. Another example could be:

    class MyAttribute: Attribute
    {
        public MyAttribute(params object[] args)
        {
        }
    }
    
    [MyAttribute("hello", 2, 3.14f)]
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
    

提交回复
热议问题