从密封派生类继承的解决方法? [英] Workaround for inheriting from sealed derived class?
问题描述
我想从派生类SealedDerived
派生,但我不能,因为该类是sealed
.如果我改为从基类Base
派生,我有什么办法可以欺骗"并将this
引用重定向到SealedDerived
类的对象?
I want to derive from a derived class SealedDerived
but I can't because the class is sealed
. If I instead derive from the base class Base
, is there any way I can "cheat" and redirect the this
reference to an object of the SealedDerived
class?
例如,像这样:
public class Base { ... }
public sealed class SealedDerived : Base { ... }
public class MyDerivedClass : Base
{
public MyDerivedClass()
{
this = new SealedDerived(); // Won't work, but is there another way?
}
}
编辑根据要求,这里是上下文:我正在将.c类库的广泛使用System.Drawing.Bitmap
移植到 Windows Store 库中.解决我在Windows Store中缺少System.Drawing.Bitmap
类的主要想法是实现一个将从WriteableBitmap
继承的虚拟Bitmap
类,从而能够返回该对象的位图对象. Windows应用商店类型.不幸的是WriteableBitmap
是sealed
.它的基类BitmapSource
(当然)不是密封的,但是另一方面,几乎没有提供处理图像的方法.因此,我陷入了困境.
EDIT Upon request, here is the context: I am porting a .NET class library that makes extensive use of System.Drawing.Bitmap
to a Windows Store library. My primary idea to workaround the lack of the System.Drawing.Bitmap
class in Windows Store was to implement a dummy Bitmap
class that would inherit from WriteableBitmap
and thereby be able to return a bitmap object of the Windows Store kind. Unfortunately WriteableBitmap
is sealed
. Its base class BitmapSource
is (of course) not sealed, but on the other hand provides practically no methods for manipulating the image. Hence my dilemma.
类似这样的东西:
using Windows.UI.Xaml.Media.Imaging;
namespace System.Drawing {
public class Bitmap : BitmapSource {
public Bitmap(int width, int height) {
this = new WriteableBitmap(width, height); // Will not work...
...
}
}
}
理想情况下,我希望伪造的Bitmap
代表 Windows Store 类型的位图类型,以便例如将伪造的Bitmap
类分配给Image.Source
. /p>
Ideally, I want my fake Bitmap
to represent a bitmap type of the Windows Store kind, so that I for example can assign my fake Bitmap
class to an Image.Source
.
推荐答案
添加为答案,因此我可以提供代码示例,但请随时作为注释.如果您觉得必须遵循这种模式,则隐式类型转换可能会为您提供帮助.不知道图像库在做什么,这只会使问题更加严重,因为无论采用哪种方法,任何Graphics.FromImage
都将永远无法正常工作.如果您的资料库仅限于GetPixel
,SetPixel
和LockBits
,则您可以花大力气进行这项工作.
Added as an answer so I could provided the code sample but feel free to take as a comment. If you feel you must keep to this pattern an implicit type cast may help you. Without knowing what your image library is doing this just pushes the problem deeper as any Graphics.FromImage
was never going to work regardless of approach. If your library is restricted to GetPixel
, SetPixel
and LockBits
you may be able to make this work with enough effort.
public class Bitmap
{
public static implicit operator WriteableBitmap(Bitmap bitmap)
{
return bitmap._internalBitmap;
}
private readonly WriteableBitmap _internalBitmap;
public Bitmap(int width, int height)
{
_internalBitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgra32, null);
}
}
public partial class MainWindow : Window
{
public Image XamlImage { get; set; }
public MainWindow()
{
var bitmap = new Bitmap(100, 100);
XamlImage.Source = bitmap;
}
}
这篇关于从密封派生类继承的解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!