对象引用未设置为对象的实例。C#接口

对象引用未设置为对象的实例。C#接口,c#,interface,C#,Interface,我知道这个问题已经被问过很多次了,我知道我之所以会出错是因为licenseinformepository为空,但我不确定如何修复它 public abstract class VendorCodeGeneratorBase { protected ILicenseInfoRepository LicenseInfoRepository; protected ICountryRegionRepository CountryRegionRepository; protect

我知道这个问题已经被问过很多次了,我知道我之所以会出错是因为
licenseinformepository
为空,但我不确定如何修复它

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);
我不知道如何实例化实现接口的类。这是一节课

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();
    }
}

在您拥有
licenseinformepository.cs
的同一文件夹中,您需要一个接口类
licenseinformepository.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
    }
}
使用系统;
使用System.Collections.Generic;
使用系统数据;
使用System.Web.Mvc;
//根据需要添加/删除
名称空间YourProject.DAL//更改项目/存储库文件夹名称
{
公共接口信息存储库:IDisposable
{
列出GetSubscriptionBrandsForVendorCode(int-keyNumber);
//添加licenseinformepository.cs文件中存在的任何其他方法
}
}

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

您是否有
IEstDbContext
的实例?您尚未初始化LicenseInfoRepository@CodeNameJack我就是这么想的,但我似乎在做些什么wrong@JasonStoner注意LicenseInformationPository的构造函数需要传入一个IEstDbContext来实例化它。@因此,当我尝试传入它时,会出现错误“字段初始值设定项无法引用非静态字段、方法或属性”我用我的接口类编辑了原始问题。
namespace Data.Repositiories
{
    public interface ILicenseInfoRepository
    {
        List<VendorCodeSubscriptionBrandModel> 
GetSubscriptionBrandsForVendorCode(int keyNumber);
    }
}
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
    }
}