c# - Extending the base class of a hierarchy tree
问题描述
How can I extend a base class of a hierarchy tree to provide extra functionality based on encapsulated (protected
values) where all children of this base class can benefit from these extensions?
Consider the following hierarchy of types.
The dots indicate more classes that introduce extra types (<T1, T2, T3>
, then <T1, T2, T3, T4>
etc).
If you want to use this code, you should extend one of the lower classes (either SimpleValidator
or SectionedValidator
) and insert the model(s) you want to validate as the type argument(s).
Now a user might decide they want some extra functionality on the Validator
class, i.e. the base class. But since everything is encapsulated, you cannot create extension methods since they can only access public properties/methods.
Extending Validator
by inheriting from it is also a no-go because that would mean users will have to go through the whole boiler-plating themselves by writing all the typed variants again.
I'd ideally like to inject a class in-between Validator
and Validator<...>
, but I don't think this is possible in C# in an elegant/user-friendly way.
解决方案
推荐阅读
- python-3.x - Pillow 和 tkinter 未显示 RGBA 图像
- c# - Blazor UI 在内存文件上传期间挂起
- android - 如何在 React-Native 中获取访问 Android Image Gallery 的权限?
- android - Flutter:如何安排通知?
- reactjs - 为什么不通过以下代码中的道具工作?
- javascript - 将消息发布到 iframe 的正确方法是什么?
- ruby-on-rails - Rails 6 ActionMailer 参数在预览中为零
- javascript - 菜单不应该中断
- c++ - 使用 OpenCV 将 3D 点投影到不失真的 ROI
- apache-spark - Spark 1.6 - Unionall 操作的替代解决方案?