说明:
例如我在使用Unity的MenuItem特性时,报了错"特性实参必须是特性形参类型的常量表达式、typeof 表达式或数组创建表达式"
经过几次定位,原因是因为我的字符串不是用const定义的,对于特性来说,实参必须是常量值,而我定义的是变量;另外加readonly声明也是没有用的,原因可以参见:C#知识系列:readonly与const的区别
public static string CustomEidorMenu = "CustomEidorMenu";
public static readonly string CustomEidorMenu = "CustomEidorMenu";
经过试验下面的一行代码也是不行的,在C#中,只有"+"进行的字符串拼接字符串常量是可以在编译期间就确定的,其他的都是动态拼接
[MenuItem($"{EditorConfig.CustomEidorMenu}/window")]
所以解决方案如下,编译通过
public const string CustomEidorMenu = "CustomEidorMenu";
[MenuItem(EditorConfig.CustomEidorMenu + "/window")]