Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>AFAIK, named parameters only permit integral types. Unfortunately i do not have a reference to back this up, I only learnt it through my own experimentation.</p> <p>When trying to use object initialisers, I got this error from the compiler:</p> <blockquote> <p>An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type</p> </blockquote> <p>Although this <a href="http://msdn.microsoft.com/en-us/library/aa288454%28v=vs.71%29.aspx" rel="nofollow">documentation</a> is a few years old, it has the reference information I was looking for:</p> <blockquote> <p>Attribute parameters are restricted to constant values of the following types:</p> <ul> <li>Simple types (bool, byte, char, short, int, long, float, and double)</li> <li>string</li> <li>System.Type</li> <li>enums </li> <li>object (The argument to an attribute parameter of type object must be a constant value of one of the above types.) One-dimensional arrays of any of the above types</li> </ul> </blockquote> <p>So this works:</p> <pre><code>//Test attribute class [AttributeUsage(AttributeTargets.All)] internal class TestAttribute : Attribute { public int[] Something { get; set; } } //Using an array initialiser - an array of integers [TestAttribute(Something = new int[]{1, 2, 3, 4, 5})] public abstract class Something </code></pre> <p>Whereas this wouldn't:</p> <pre><code>//Test person class internal class Person { public string Name { get; set; } public Person(string name) { this.Name = name; } } //Test attribute class [AttributeUsage(AttributeTargets.All)] internal class TestAttribute : Attribute { public IEnumerable&lt;Person&gt; Something { get; set; } } //This won't work as Person is not an integral type [TestAttribute(Something = new Person[]{new Person("James")})] </code></pre> <p>EDIT: just to elaborate, attributes form part of the metadata for constructs they are applied to (within the generated IL), thus the members of the attribute class must be determined at compile time; hence the restriction on attribute parameters to constant values.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload