我正在修改位于两个既定层之间的一些代码,并且无法确定最佳设计是什么。
当前代码调用文件访问库,并将对象返回给调用者。我需要扩展返回的对象以添加一些自定义处理功能。我无权访问传递对象的定义(例如,有些是文件流)
如果我可以创建一个行为类似于基本实例的子实例,并且可以从基本实例创建,但它具有一些隐藏的额外功能,这将为我节省大量工作。这可以在不知道基类的实现细节的情况下完成吗?
在看起来很像这样的代码形式中:
private class FileObjectWithDispose : FileObject, IDisposable
{//voidaction is a delegate, this is .net 2.0
private VoidAction _disposeCallback;
public static FileObjectWithDispose wrapFile(FileObject unWrappedFile, VoidAction DisposeAction)
{//Implementation missing, this is the crux of what I don't know how to do
FileObjectWithDispose wrappedFile = new FileObjectWithDispose(unWrappedFile);
wrappedFile._disposeCallback = DisposeAction;
return wrappedFile;
}
private FileObjectWithDispose()
: base(null, null)//base class does not have a default constructor
{
throw new NotImplementedException("FileObjectWithDispose is a wrapper class which is intended only to be cast …Run Code Online (Sandbox Code Playgroud)