下面的代码是随机来源的,在.Net框架中,我正在尝试将它转换为.Net 6。由于我对.Net 6并不熟悉,所以我无法确定在迁移过程中应该重用哪些语句。我尝试使用.Net 6样式来获取我的设置键和值,但是我得到了objt ref is required for non-static异常。
public abstract class ServiceBase : IDisposable
{
//newly added
//start
private readonly PrintSettings _PrintSettings;
public ServiceBase(IOptions<PrintSettings> options)
{
_PrintSettings = options.Value;
}
//end
// Problem is here, object ref required for non static.
public ServiceBase() : this(_PrintSettings.DBConnection()) { }
protected ServiceBase(string instance)
{
this.proxy = new Proxy(instance);
}
public void BeginTran()
{
this.proxy.GetContext().BeginTran();
}发布于 2022-09-19 07:23:09
所有现代C#平台都实现了.Net Standard规范。要理解如果您只需将代码从一个平台复制到另一个平台,您应该检查这些平台支持的.Net Standard版本。.Net Faramework从4.5版开始就实现了它。.Net 6支持所有.Net standard版本。这意味着,如果.Net Framework版本高于4.5,您可以轻松地将代码从其项目复制到.Net 6项目。
你可以在这里读到更多关于它的https://learn.microsoft.com/en-us/dotnet/standard/net-standard?tabs=net-standard-1-0。
但是请记住,如果平台没有通用的.Net Standard实现,这并不意味着您不能将粘贴代码从一个复制到另一个。这仅仅意味着这些平台有一些不同的功能。通常,您可以复制代码并检查其编译错误。如果没有错误,那么可以随意使用此代码。
谈到object ref错误。您试图使用_PrintSettings字段作为构造函数参数,但在构造对象之前不能使用字段。您应该更好地考虑ServiceBase瞬间的生命周期,并确定创建这些对象到底需要什么。并将此依赖项传递给构造函数。
https://stackoverflow.com/questions/73769384
复制相似问题