继承基类错误不包含获取0个争论的构造函数



我试图将基类继承到我的一个类中,以便我可以使用其受保护的功能。基类是在VB中内置的,这是项目中的参考,如下:

Public MustInherit Class RequestLifetimeCacheBase
Private ReadOnly _requestContext As HttpContextBase
Public Sub New(requestContext As HttpContextBase)
    Me._requestContext = requestContext
End Sub
Public Sub New(controller As Controller)
    Me.New(controller.HttpContext)
End Sub
Protected Function GetCachedValue(Of TValue As Structure)(cacheKey As String) As TValue?
    Dim value = Me._requestContext.Items(cacheKey)
    If TypeOf value Is TValue Then
        Return DirectCast(value, TValue)
    Else
        Return Nothing
    End If
End Function
Protected Sub SetCachedValue(Of TValue As Structure)(cacheKey As String, value As TValue?)
    If value.HasValue Then
        Me._requestContext.Items(cacheKey) = value.Value
    Else
        Me._requestContext.Items.Remove(cacheKey)
    End If
End Sub

Protected Function GetCachedObject(Of TObject As Class)(cacheKey As String) As TObject
    Return TryCast(Me._requestContext.Items(cacheKey), TObject)
End Function
Protected Sub SetCachedObject(Of TObject As Class)(cacheKey As String, value As TObject)
    If value IsNot Nothing Then
        Me._requestContext.Items(cacheKey) = value
    Else
        Me._requestContext.Items.Remove(cacheKey)
    End If
End Sub
End Class

正在创建VB中的旧方法:

 Dim cache = New RequestLifetimeCache(Me)
        Dim cachedDatabase = cache.CustomerDatabase
        If cachedDatabase IsNot Nothing Then
            Return cachedDatabase
        End If

我在C#中创建了这样的新实例:

 public WebsiteDatabaseContext customerDB
    {
        get
        {
            RequestLifetimeCache cache = new RequestLifetimeCache();
            var cachedDB = cache.CustomerDatabase;
            return cachedDB;
        }
        set
        {
        }
    }

我的类代码如下:

 public class RequestLifetimeCache : RequestLifetimeCacheBase
{
    public RequestLifetimeCache()
    {
    }
    public WebsiteDatabaseContext CustomerDatabase
    {
        get
        {
            return base.GetCachedObject<WebsiteDatabaseContext>("Customer database");
        }
    }
}

但是我遇到的错误'requestLifetimecachebase'不包含在以下行上进行0参数的构造函数:

    public RequestLifetimeCache()

我在做什么错?我是否错过了传递httpcontext/控制器参数或其他内容?

VB在.NET框架上,我正在处理.NET Core

您需要传递构造函数参数。我认为这样(我不知道VB(:

public class RequestLifetimeCache : RequestLifetimeCacheBase
{
    public RequestLifetimeCache(HttpContextBase ctx) : base(ctx)
    {
    }
    public RequestLifetimeCache(Controller controller) : base(controller)
    {
    }
    public WebsiteDatabaseContext CustomerDatabase
    {
        get
        {
            return base.GetCachedObject<WebsiteDatabaseContext>("Customer database");
        }
    }
}

我不得不将C#中的VB基类重写为临时度量:

 public abstract class RequestLifetimeCacheBase
 {
    private readonly HttpContext _requestContext;
    protected RequestLifetimeCacheBase(HttpContext requestContext)
    {
        _requestContext = requestContext;
    }
    protected RequestLifetimeCacheBase(Controller controller)
        : this(controller.HttpContext)
    {
    }
    #region Generic access to cached value-type data
    protected TValue? GetCachedValue<TValue>(string cacheKey)
        where TValue : struct
    {
        var value = _requestContext.Items[cacheKey];
        if (value is TValue)
        {
            return (TValue)value;
        }
        else
        {
            return null;
        }
    }
    protected void SetCachedValue<TValue>(string cacheKey, TValue? value)
        where TValue : struct
    {
        if (value.HasValue)
        {
            _requestContext.Items[cacheKey] = value.Value;
        }
        else
        {
            _requestContext.Items.Remove(cacheKey);
        }
    }
    #endregion
    #region Generic access to cached reference-type data
    protected TObject GetCachedObject<TObject>(string cacheKey)
        where TObject : class
    {
        return _requestContext.Items[cacheKey] as TObject;
    }
    protected void SetCachedObject<TObject>(string cacheKey, TObject value)
        where TObject : class
    {
        if (value != null)
        {
            _requestContext.Items[cacheKey] = value;
        }
        else
        {
            _requestContext.Items.Remove(cacheKey);
        }
    }
    #endregion
}

相关内容

  • 没有找到相关文章

最新更新