5 回答

TA貢獻(xiàn)2016條經(jīng)驗 獲得超9個贊
不確定這是否與我們的情況相同,但我們有一個類可以從配置中訪問密鑰
string sampleString = WebConfigurationManager.AppSettings["SampleString"]
由于一些合并問題,示例字符串已從我們的 web.config 中刪除。當(dāng)您訪問變量下面的類中的任何變量時,會發(fā)生空指針錯誤。
在配置中添加密鑰解決了這個問題。

TA貢獻(xiàn)2003條經(jīng)驗 獲得超2個贊
當(dāng)我運行此代碼時:
void Main()
{
Console.WriteLine(Constants.sampleList.Contains(1));
}
public static class Constants
{
public static string sampleString= "";
public static List<int> sampleList= new List<int> {1,2,3};
}
我進(jìn)入True控制臺。您需要提供演示您所面臨問題的代碼。

TA貢獻(xiàn)2039條經(jīng)驗 獲得超8個贊
如果您readonly
在方法中添加關(guān)鍵字。該方法只能在構(gòu)造函數(shù)內(nèi)部或在屬性聲明階段進(jìn)行實例化。
public static readonly List<int> sampleList= new List<int> {1,2,3};
如果您嘗試重新初始化或?qū)?進(jìn)行新分配sampleList
,C# 編譯器會給您編譯器錯誤。
更好地使用屬性
public static readonly List<int> SampleList {get; set;} = new List<int> {1,2,3};

TA貢獻(xiàn)1155條經(jīng)驗 獲得超0個贊
沒有奇跡,如果sampleList是null(并且您拋出了異常),那么您null在某處分配給它。盡量不要暴露 易受攻擊的public 字段:Constants
public static class Constants
{
public static string sampleString = "";
public static List<int> sampleList = new List<int> {1,2,3};
}
...
Constants.sampleList = null; // We can easly assign null
...
Constants.sampleList.Add(123); // <- And have an unxpected exception
但是要么把它們變成屬性:
public static class Constants
{
private static string s_SampleString = "";
private static List<int> s_SampleList = new List<int> {1,2,3};
public static string sampleString {
get {return s_SampleString;}
set {s_SampleString = value ?? "";}
}
public static List<int> sampleList {
get {return s_SampleList;}
// I doubt you actually want set here
set {s_SampleList = value ?? new List<int>();}
}
}
或者,至少,將字段標(biāo)記為readonly(您只能分配一次):
public static class Constants
{
private static string s_SampleString = "";
public static string sampleString {
get {return s_SampleString;}
set {s_SampleString = value ?? "";}
}
// Manipulate with me, but not reassign
public static readonly List<int> sampleList = new List<int> {1,2,3};
}
無論哪種情況,您仍然可以使用列表進(jìn)行操作:
Constants.sampleList.Add(4);
Constants.sampleList.RemoveAt(0);
但是您可以避免分配null給列表:將分配空列表(帶有屬性的代碼),或者您將遇到編譯時錯誤(帶有 的代碼readonly)

TA貢獻(xiàn)1826條經(jīng)驗 獲得超6個贊
在我的例子中,我private static readonly Dictionary<byte, string>在一個結(jié)構(gòu)內(nèi)部定義了一個來保存預(yù)定義的常量。這通??梢哉9ぷ鳎俏疫€在我的結(jié)構(gòu)中定義了一個 MinValue 來表示一條數(shù)據(jù)的最小值。以這種方式完成時,靜態(tài)字典未初始化,除非在靜態(tài) MinValue 屬性上方定義。我可能對編譯器的要求太多了,應(yīng)該對其進(jìn)行重構(gòu)。
在大型結(jié)構(gòu)中很難診斷,因為我沒想到 C# 會出現(xiàn)這種行為。重現(xiàn)示例:
public struct MyStruct
{
public string Str;
public static readonly MyStruct MinValue = new MyStruct(0);
public MyStruct(byte val)
{
Str = _predefinedValues[val]; // null reference exception
}
private static readonly Dictionary<byte, string> _predefinedValues = new Dictionary<byte, string>()
{
{0x00, "test 1"},
{0x01, "test 2"},
{0x02, "test 3"},
};
}
這里的解決方案是雙重的:
不調(diào)用結(jié)構(gòu)上的任何構(gòu)造函數(shù)并在不訪問
_predefinedValues
列表的情況下顯式設(shè)置每個字段。或者,將列表向上移動到 MinValue 字段聲明之上實際上也可以修復(fù)它(奇怪吧?)
我懷疑在嘗試分配它時堆棧上發(fā)生了一些奇怪的事情,這可能是一件奇怪的事情。
- 5 回答
- 0 關(guān)注
- 157 瀏覽
添加回答
舉報