gpt4 book ai didi

c# - 在字段具有值之前拒绝使用方法

转载 作者:行者123 更新时间:2023-11-30 17:40:21 25 4
gpt4 key购买 nike

我敢打赌这是一个非常基本的设计问题,但由于我很难用措词来表达它,所以我无法在网上找到任何资源来帮助我回答它。我正在使用 C#6,虽然这是一个设计问题,但我也接受任何使用 c#6 独特功能的答案。

我的问题:假设您有一个简单的类 MyClass,其属性为 MyPropMyClass 还有方法:

public void InitializeMyProp(someValue)
{
MyProp = //use the value of someValue to calculate MyProp
}

此外,MyClass 有许多使用 MyProp 的公共(public)方法,例如:

public int DependedMethod1()
{
//use the value of MyProp to do some calculations. this method might also fail to initialize MyProp and I'm not sure how to handle this situation
return valueThatDependOnMyProp1;
}

MyProp 的值在实例化 MyClass 时是未知的。但是,方法 DependedMethod1、DependedMethod2、...、DependedMethodN 都依赖于 MyProp 的值。

我的问题:如何确保 MyProp 在使用前已初始化?

我有一些想法,所有这些我都不喜欢:

1.当然,我可以在每个方法DependedMethod中添加相同的代码:

if(MyProp == null) 抛出异常()

但这有三个缺点:a。重复代码? b.每次我调用该方法时都需要处理异常。

  1. 与我的第一个(坏)想法相关,也许可以阻止事先调用这些方法?比如说,将所有 DependedMethods 放在一个内部(静态?)类 InnerDependedClass 中,分配给 MyClass 类型为 InnerDependedClass 的字段>(我们将其命名为 MyField)并在 InitializeMyProp 实例化它。

这是一个合理的解决方案,因为如果 InitializeMyProp 失败,我不会实例化 MyField。但问题仍然存在 - 如果其他类想要使用 MyField 的方法,它必须事先检查它是否为 null - 这有点像询问 MyProp 是否为空。

我仍在努力在这里充分解释我自己。

我的具体问题是:给定一个二进制文件路径,打开文件,读取其中的前 2 个字节(一种偏移键),然后保存该键。将文件保存在内存中(我正在使用 InMemoryFileStream),因为源可能会被删除或更改。稍后,当被询问时,使用键(同样是偏移量)从 fileStream 中提取特定信息。问题是,如果文件已损坏/无法打开进行读取(出于任何原因),则 key 将为空。

我相信我这里有一个很大的设计问题。我的代码如下

public class MyReaderStream : IDisposable, IMyReaderStream
{
public MemoryStream InMemoryStream { get; }

public MyReaderStream(string path)
{
InMemoryStream = GetInputFile(path);
//if (InMemoryStream == null) throw new MyReaderException($"path {path} could not be found");
if (InMemoryStream == null) InMemoryStream = new MemoryStream();
}

public MyReaderStream(MemoryStream stream)
{
InMemoryStream = stream;
if (InMemoryStream == null) InMemoryStream = new MemoryStream();
}

public void Dispose()
{
InMemoryStream?.Dispose();
}

public bool VerifyPath(string path)
{
var extension = Path.GetExtension(path);
if (extension != null && !extension.Equals(@".bin")) return false;
return File.Exists(path);
}

public MemoryStream GetInputFile(string path)
{
if (!VerifyPath(path)) return null;

MemoryStream inMemoryCopy = new MemoryStream();

using (FileStream fs = File.OpenRead(path))
{
fs.CopyTo(inMemoryCopy);
}

return inMemoryCopy;
}
}
public class MyReader
{
private readonly IMyReaderStream _stream;
private uint? offset = null;

public MyReader(IMyReaderStream stream)
{
this._stream = stream ?? new MyReaderStream(new MemoryStream());
}

public uint? GetKey()
{
try
{
offset = Helpers.ReadWord(_stream.InMemoryStream, 0);
//might fail if file is corrupted

}
catch(Exception) {return null;}
}
public uint? FirstDependedMethod(){
return Helpers.ReadWord(_stream.InMemoryStream, offset + SOME_CONSTANT1); //if offset is null...?
}
//more methods like FirstDependedMethod
//.....
//.....
}

更新

这是一个 WPF MVVM 项目。模型看起来像这样:

[NotifyPropertyChanged]
public class Model
{
private NvmReader _reader;

public Model()
{
Reader = new NvmReader(null);
}

public NvmReader Reader
{
set { _reader = value; }
get { return _reader; }
}
}

并由 ViewModel 初始化,如下所示:

this._model = new Model();    

当用户选择二进制文件的路径时,NvmReader 对象才能获得所需的偏移量

最佳答案

你应该把类(class)分成两个类(class)。

假设你从一门课开始

public class MyClass
{
public void Initialize(int value)
{

}

public void MethodA() { }
public void MethodB() { }
}

MethodAMethodB 都要求首先调用Initialize 方法。

您可以将此类重构为 2 个单独的类。一个负责实现MethodAMethodB,另一个负责初始化。这样类型系统将确保一切都已初始化。

public class MyClassFactory
{
public MyClass Create(int value)
{
return new MyClass(value);
}
}

public class MyClass
{
internal MyClass(int value)
{

}

public void MethodA() { }
public void MethodB() { }
}

关于c# - 在字段具有值之前拒绝使用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34401921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com