字段初始值设定项不能引用非静态字段、方法或 属性 但已在 ctor 中初始化

A field initializer cannot reference the nonstatic field, method, or property but already initialized in ctor

我无法使用 _cloudinaryConfig 因为这个错误:

A field initializer cannot reference the nonstatic field, method, or property

我不确定应该做什么

 private IAppRepository _appRepository;
 private IMapper _mapper;
 private IOptions<CloudinarySettings> _cloudinaryConfig;

 public PhotosController(
        IAppRepository appRepository,
        IMapper mapper,
        IOptions<CloudinarySettings> cloudinaryConfig)
 {
     this._appRepository = appRepository;
     this._mapper = mapper;
     this._cloudinaryConfig = cloudinaryConfig;
 }

 string cloud = _cloudinaryConfig.Value.CloudName;
 string apiKey = _cloudinaryConfig.Value.ApiKey;
 string apiSecret = _cloudinaryConfig.Value.ApiSecret;

 Cloudinary _cloudinary = new Cloudinary(new Cloudinary(
        new Account(cloud, apiKey, apiSecret)));

你的意思是这样的?

 private IAppRepository _appRepository;
 private IMapper _mapper;
 private IOptions<CloudinarySettings> _cloudinaryConfig;

 string cloud;
 string apiKey;
 string apiSecret;

 Cloudinary _cloudinary;

 public PhotosController(
        IAppRepository appRepository,
        IMapper mapper,
        IOptions<CloudinarySettings> cloudinaryConfig)
 {
     this._appRepository = appRepository;
     this._mapper = mapper;
     this._cloudinaryConfig = cloudinaryConfig;
     this.cloud = _cloudinaryConfig.Value.CloudName;
     this.apiKey = _cloudinaryConfig.Value.ApiKey;
     this.apiSecret = _cloudinaryConfig.Value.ApiSecret
     this._cloudinary = new Cloudinary(new Cloudinary(
        new Account(cloud, apiKey, apiSecret)));
 }