以下是我的代码:
private BitsManager manager;
private const string DisplayName = "Test Job";
public SyncHelper()
{
manager = new BitsManager();
}
BitsJob uploadBitsJob = manager.CreateJob(DisplayName, JobType.Upload);
我收到以下错误:
A field initializer cannot reference the non-static field, method, or property 'BITSIntegrationModule.SyncService.SyncHelper.manager'
最佳答案
线
BitsJob uploadBitsJob = manager.CreateJob(DisplayName, JobType.Upload);
无法访问管理器,因为尚未将其设置为任何值-您可以将分配移至构造函数中-
private readonly BitsManager manager;
private const string DisplayName = "Test Job";
BitsJob readonly uploadBitsJob;
public SyncHelper()
{
manager = new BitsManager();
uploadBitsJob = manager.CreateJob(DisplayName, JobType.Upload);
}
关于c# - 字段初始值设定项不能引用非静态字段,方法或属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15204420/