我已經從MVC控制器 -甲字段初始不能引用非靜態字段,方法或特性在ASP.Net MVC控制器
public abstract class BaseController : Controller
{
protected int A= Convert.ToInt32(ConfigurationManager.AppSettings["A"]);
protected int B= Convert.ToInt32(ConfigurationManager.AppSettings["B"]);
protected int C= A* B;
}
繼承了BaseController類它編譯期間給出錯誤A field initializer cannot reference the non-static field, method, or property
。
我沒有任何構造函數。這就是這個類的完整代碼。 有什麼幫助嗎?
你可以在構造函數中初始化C嗎?在實例化過程中搜索字段初始值設定項和構造函數的順序,這個限制與該主題有關。 –
你能顯示BaseController的完整代碼嗎?你有沒有定義這個控制器的任何構造函數? –