以下是我的代码:
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);
}
uploadBitsJob
是在一流水平,这使得它的字段声明。 现场实例无法用于初始化等领域。
相反,你可以声明栏没有初始化它:
BitsJob uploadBitsJob;
然后初始化在构造函数中的字段:
public SyncHelper()
{
manager = new BitsManager();
uploadBitsJob = manager.CreateJob(DisplayName, JobType.Upload);//here. Now manager is initialized
}
,试图从静态方法访问非静态属性时,通常会发生。 请提供更多的代码。
文章来源: A field initializer cannot reference the non-static field, method, or property