AFAIK,命名參數只允許整數類型。不幸的是,我沒有提及支持這一點,我只是通過我自己的實驗來了解它。
當試圖使用對象initialisers,我從編譯此錯誤:
的屬性參數必須是常量表達式的typeof屬性參數類型的表達或數組創建表達式
雖然這documentation是幾歲,它有我正在尋找的參考信息:
屬性p arameters被限制在 以下類型的恆定值:
- 簡單類型(布爾,字節,字符,短,整型,長整型,浮點,雙)
- 串
- 的System.Type
- 枚舉
- object(對象類型的屬性參數的參數必須是上述類型之一的常量值。)的任何上述類型
的一維數組 所以此工程:
//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
雖然這不會:
//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<Person> Something { get; set; }
}
//This won't work as Person is not an integral type
[TestAttribute(Something = new Person[]{new Person("James")})]
編輯:只是爲了闡述,屬性構成它們應用到的構造的元數據的一部分(在生成的IL中),因此屬性類的成員必須在編譯時確定;因此將屬性參數限制爲常量值。
我曾經在一個屬性中放入了一個'string []'初始值設定項。 – JohnL