我試圖做到以下幾點:C# - 全局內部常量?
[FooAttribute(Value = String.Format("{0} - {1}", myReources.BaseString, "Bar"))]
public int FooBar { get; set; }
編譯器會抱怨,但...那麼是什麼做的,我有我的一個位置BaseString
正確的方法是什麼?我的代碼充斥着庫中屬性的屬性,因爲我不能使用資源,所以「全局」內部常量聲音就像解決方案一樣。
我試圖做到以下幾點:C# - 全局內部常量?
[FooAttribute(Value = String.Format("{0} - {1}", myReources.BaseString, "Bar"))]
public int FooBar { get; set; }
編譯器會抱怨,但...那麼是什麼做的,我有我的一個位置BaseString
正確的方法是什麼?我的代碼充斥着庫中屬性的屬性,因爲我不能使用資源,所以「全局」內部常量聲音就像解決方案一樣。
你不能在屬性類似的String.format表情......但下面應該工作:
public class MyResources
{
public const string BaseString = "there";
}
[FooAttribute(Value = MyReources.BaseString + " - Bar"))]
public int FooBar { get; set; }
我想這就是......真正獲得編譯時常量的唯一方法...... – michael
如果刪除的String.Format和使用基本字符串連接,編譯器將不抱怨。由於String.Format在運行時被解析並且不是編譯時,所以不能在屬性中使用它。編譯器會認識到myResources.BaseString和「Bar」都是常量值,所以這樣做是合法的。
[FooAttribute(Value = myReources.BaseString + "Bar")]
其實,如果人們所說的是正確的(它需要編譯時常量),那麼我可以'根本不使用資源,因爲它們使用默認的ResX Generator作爲屬性公開。 – michael
而且,編譯器究竟抱怨什麼? –
另外,在屬性中你不能有像'String.Format'這樣的表達式,它們必須是編譯時間常量。我不知道一個'const'變量是否值得這個。 –