我尝试在 Nop.Services.Customers.CustomerService 中创建静态函数以获取
nop数据库中的客户列表。我想在外部控制台中调用此函数
的应用。但 CustomerService 类不包含默认构造函数。
请参阅构造函数代码。
#region Ctor
/// <summary>
/// Ctor
/// </summary>
/// <param name="cacheManager">Cache manager</param>
/// <param name="customerRepository">Customer repository</param>
/// <param name="customerRoleRepository">Customer role repository</param>
/// <param name="customerAttributeRepository">Customer attribute repository</param>
/// <param name="encryptionService">Encryption service</param>
/// <param name="newsLetterSubscriptionService">Newsletter subscription service</param>
/// <param name="rewardPointsSettings">Reward points settings</param>
/// <param name="customerSettings">Customer settings</param>
/// <param name="eventPublisher"></param>
public CustomerService(ICacheManager cacheManager,
IRepository<Customer> customerRepository,
IRepository<CustomerRole> customerRoleRepository,
IRepository<CustomerAttribute> customerAttributeRepository,
IEncryptionService encryptionService, INewsLetterSubscriptionService newsLetterSubscriptionService,
RewardPointsSettings rewardPointsSettings, CustomerSettings customerSettings,
IEventPublisher eventPublisher)
{
_cacheManager = cacheManager;
_customerRepository = customerRepository;
_customerRoleRepository = customerRoleRepository;
_customerAttributeRepository = customerAttributeRepository;
_encryptionService = encryptionService;
_newsLetterSubscriptionService = newsLetterSubscriptionService;
_rewardPointsSettings = rewardPointsSettings;
_customerSettings = customerSettings;
_eventPublisher = eventPublisher;
}
#endregion
当尝试调用静态函数时,Fileds会显示错误。
请参阅字段
#region Fields
private readonly IRepository<Customer> _customerRepository;
private readonly IRepository<CustomerRole> _customerRoleRepository;
private readonly IRepository<CustomerAttribute> _customerAttributeRepository;
private readonly IRepository<FileUpload> _fileuploadRepository;
private readonly IEncryptionService _encryptionService;
private readonly ICacheManager _cacheManager;
private readonly INewsLetterSubscriptionService _newsLetterSubscriptionService;
private readonly RewardPointsSettings _rewardPointsSettings;
private readonly CustomerSettings _customerSettings;
private readonly IEventPublisher _eventPublisher;
#endregion
我在CustomerService类中创建了一个默认值。
public CustomerService()
{
}
并在CustomerService中创建新功能
public virtual List<Customer> GetClients()
{
var _cust = _customerRepository.Table;
return _cust.ToList();
}
并在外部控制台应用程序中调用此函数
private static CustomerService _customerService = new CustomerService();
static void Main(string[] args)
{
List<Customer> cust = _customerService.GetClients();
ThreadStart start = new ThreadStart(ProcessMails);
thread = new Thread(start);
ProcessStatus = 1;
thread.Start();
}
但是当我调用此函数时,它会显示空错误。
在Nop.Core中创建一个函数并在外部应用程序中调用是不可能的?
请帮忙。
答案 0 :(得分:2)
从课程定义来看,这是不可能的。静态方法适用于类型,而不适用于实例,因此可以在静态方法中使用的成员变量也应该是静态的。