我有以下类与PropertyGrid的数据对象一起使用:
public enum WebUILanguage
{
EnglishUS,
German,
//Actual list is obviously longer
}
[DefaultPropertyAttribute("SaveOnClose")]
public class MyData
{
private HashSet<WebUILanguage> _SupportedUILanguages = new HashSet<WebUILanguage>(_k_SupportedUILanguages);
//Area to define all default values
const WebUILanguage[] _k_SupportedUILanguages = { //Error CS0134: A const field of a reference type other than string can only be initialized with null.
WebUILanguage.EnglishUS,
WebUILanguage.German
};
[CategoryAttribute("User Interface"),
DefaultValueAttribute(_k_SupportedUILanguages),
DescriptionAttribute("Supported user interface languages.")]
[RefreshProperties(RefreshProperties.All)]
public WebUILanguage[] SupportedUILanguages
{
get { return _SupportedUILanguages.ToArray(); }
set
{
//Do 'set' magic here
}
}
}
但编译器不允许我初始化一个常量数组 - 上面代码中的_k_SupportedUILanguages
常量。这是什么语法?
PS。请注意,我不需要一个readonly
数组。它必须是常量!