繁体   English   中英

你调用的对象是空的。 C#接口

[英]Object reference not set to an instance of an object. C# Interface

我知道这个问题已经问过很多次了,我知道收到此错误的原因是因为LicenseInfoRepository为null,但是我不确定如何解决它。

public abstract class VendorCodeGeneratorBase
{

    protected ILicenseInfoRepository LicenseInfoRepository;
    protected ICountryRegionRepository CountryRegionRepository;
    protected IEnumerable<IBrandModel> BrandModels;

    protected string GenerateVendorCodeUnEncoded(GenerateVendorCodeRequestModel requestModel)
    {
        int formatId = requestModel.VendorCodeFormat;
        var strToConvert = "";
        var brandBytes = new byte[5];
        var brands = LicenseInfoRepository.GetSubscriptionBrandsForVendorCode(requestModel.KeyNumber);

错误正在发生

var brands = LicenseInfoRepository.GetSubscriptionBrandsForVendorCode(requestModel.KeyNumber);

我不知道如何实例化实现该接口的类。 这是班

public class LicenseInfoRepository : ILicenseInfoRepository
{
    private readonly IEstDbContext _estDbContext;

    public LicenseInfoRepository(IEstDbContext estDbContext)
    {
        _estDbContext = estDbContext;
    }

    public List<VendorCodeSubscriptionBrandModel> 
    GetSubscriptionBrandsForVendorCode(int keyNumber)
    {
        return _estDbContext.SubscriptionBrands
            .Include(sb => sb.Brand)
            .Where(sb => sb.KeyNumber == keyNumber)
            .ProjectTo<VendorCodeSubscriptionBrandModel>()
            .ToList();
    }
}

我有一个名为repositories的文件夹,里面有一个LicenseInfoRepository.cs类,里面还有一个名为Interfaces的文件夹,它具有ILicenseInfoRepository.cs接口。 该接口如下所示:

namespace Data.Repositiories
{
    public interface ILicenseInfoRepository
    {
        List<VendorCodeSubscriptionBrandModel> 
GetSubscriptionBrandsForVendorCode(int keyNumber);
    }
}

在拥有LicenseInfoRepository.cs的同一文件夹中,需要一个接口类ILicenseInfoRepository.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Web.Mvc;
// Add/remove as appropriate

namespace YourProject.DAL // Change to your project / repository folder name
{
    public interface ILicenseInfoRepository : IDisposable
    {
        List<VendorCodeSubscriptionBrandModel> GetSubscriptionBrandsForVendorCode(int keyNumber);
        // Add any other methods that exist in LicenseInfoRepository.cs file
    }
}

VendorCodeGeneratorBase类中可能缺少行,尤其是通常如何实例化private ApplicationDbContext db = new ApplicationDbContext(); VendorCodeGeneratorBase 添加界面后,请分享错误/详细信息,以便我们继续修复您的代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM