飞镖/反射:如何从抽象基类获取所有子类

发布于 2025-02-08 07:24:20 字数 1306 浏览 2 评论 0 原文

我想使用 dart/dofleable 框架框架以查找特定基类的所有子类,我与我斗争那个用例。

我有

  1. 一个带有几个Getters的抽象基类:
abstract class MyBaseClass {
    String get name;
    List<MyValueType> get values;
}
  1. 实现 mybaseclass 的几个类:
class A implements MyBaseClass {
   @override
   String name = 'AClass';
   
   @override
   List<MyValueType> = [MyValueType.X, MyValueType.Y]; 
}

class B implements MyBaseClass {
   @override
   String name = 'BClass';
   
   @override
   List<MyValueType> = []; 
}

我的目标是获取所有实现 mybaseclass 的类并读取其属性。

所以,我创建了:

class Reflector extends Reflectable {
  const Reflector()
      : super(invokingCapability); 
}

const reflector = const Reflector();
  1. 如何获取类列表?我只找到 instancemirror.reflect(),它仅能提供一个结果,没有多少。
  2. 尚不清楚,必须如何设置注释。尝试获取所有 mybaseclass 实现时,我是否需要注释我的摘要 mybaseclass ,还是我需要注释类 a a a 和 B 还是我需要注释这三个类?
  3. 我需要哪些功能?在我的测试案例中,我得到了以下例外: nosuchcapabilityerror:没有能力调用getter“ name” ,但无法解决此问题。

预先感谢,任何帮助将不胜感激!

I want to use the Dart/reflectable framework to find all subclasses of a specific base class and I struggle with that use case.

I have

  1. An abstract base class with a few getters:
abstract class MyBaseClass {
    String get name;
    List<MyValueType> get values;
}
  1. Several classes that implement MyBaseClass:
class A implements MyBaseClass {
   @override
   String name = 'AClass';
   
   @override
   List<MyValueType> = [MyValueType.X, MyValueType.Y]; 
}

class B implements MyBaseClass {
   @override
   String name = 'BClass';
   
   @override
   List<MyValueType> = []; 
}

My goal is to fetch all classes that implement MyBaseClass and read their properties.

So, I created:

class Reflector extends Reflectable {
  const Reflector()
      : super(invokingCapability); 
}

const reflector = const Reflector();
  1. How do I fetch a list of classes? I only found the InstanceMirror.reflect() which only delivers one result, not many.
  2. It is not clear, how the annotation must be set. When trying to fetch all MyBaseClass implementations, do I need to annotate only my abstract MyBaseClass or do I need to annotate classes A and B or do I need to annotate all three classes?
  3. Which capabilities do I need? In my test case I got this exception: NoSuchCapabilityError: no capability to invoke the getter "name" but was not able to solve this.

Thanks in advance, any help is appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心舞飞扬 2025-02-15 07:24:20

没有内置的支持来查找所有子类别,只有子类型。这意味着我们将获得两个类B1扩展A {} 类B2如果我们通过 a 的所有子类型进行量化,则实现A {} 。 ,然后,如果我们只想找到子类,我们需要过滤出每个子类型,这也不是子类。在连接是间接的情况下,只有某些连接是扩展,我们需要考虑我们想要的东西。例如,类C1扩展了B2 {} ,将其视为给定目的的 a 的子类,或者仅将类C2扩展到B1 {} 包括?

但是,似乎我们还是正在寻找所有子类型:“我的目标是获取实现 mybaseclass '的所有类。因此,我假设它只是亚型(因此,的任何组合扩展实现都可以做到)。在这种情况下,我们可以直接使用 subtepequantifyCapability

您需要指定 typerelations capability 如果要调用有关类型关系的任何镜像方法,例如, superclass issubtypeof ,也许是一个十几种其他方法。

这是一个示例:

为了检查与特定反射器 Reflector 的反射支持的集合,请使用<,使用<代码> Reflector.annotatedClasses 。

示例:

查看原始示例:为了包括类 mybaseclass 以及所有子类型(带有声明,也就是说,除了从不,但包括 a b ),必须拥有 subTypequantifyCapability ,并以某种方式包括对 mybaseclass 的支持。执行后者的明显和最直接的方法是将 @reflector 作为元数据在 mybaseclass的声明中

在避免对许多许多声明的反射支持引起的计划爆炸的必要性,在这种情况下,实际上只需要少数几个,因此,反射式的整体设计的动机。这意味着缺少功能是一个微妙而典型的问题:您需要仔细选择随附的功能,也需要仔细注释类,以避免遇到这个程序尺寸问题。在这种特定情况下,这可能并不重要,但是仅通过支持所请求的功能(通过在给定反射器类声明中包含相应的功能)来保存空间。

在这种特殊情况下,没有能力在给定的 instancemirror 上调用名为 name 的Getter(如果是实例getter), classMirror (如果是静态getter)或 librarymirror (如果是顶级getter)。我不确定这是如何发生的(因为 InvokingCapability 应该涵盖各种调用),但是请尝试修复其他事情,然后如果此问题持续存在,则返回。

There is no built-in support for finding all subclasses, only subtypes. This means that we'll get both class B1 extends A {} and class B2 implements A {} if we quantify over all subtypes of A, and then we'll need to filter out each subtype which is not also a subclass, if we wish to find just the subclasses. We'd need to think about what we want in the case where the connection is indirect and only some of the connections are extends. E.g., class C1 extends B2 {}, does that count as a subclass of A for the given purpose, or would only class C2 extends B1 {} be included?

But it seems like we're looking for all subtypes anyway: 'My goal is to fetch all classes that implement MyBaseClass'. So I'll assume that it's just about subtypes (so any combination of extends and implements will do). In that case we can directly use a subtypeQuantifyCapability.

You'll need to specify typeRelationsCapability if you want to call any mirror methods dealing with type relations, e.g., superclass, isSubtypeOf, and perhaps a dozen other methods.

Here's an example: https://github.com/google/reflectable.dart/blob/master/test_reflectable/test/subtype_quantify_test.dart

In order to inspect the set of classes for which there is reflection support with a specific reflector reflector, use reflector.annotatedClasses.

Example: https://github.com/google/reflectable.dart/blob/master/test_reflectable/test/annotated_classes_test.dart.

Looking at the original example: In order to include the class MyBaseClass as well as all subtypes (with declarations, that is, except Never, but including A and B), it is necessary to have the subtypeQuantifyCapability and somehow include support for MyBaseClass. The obvious and most direct way to do the latter is to have @reflector as metadata on the declaration of MyBaseClass.

The whole design of reflectable was motivated by the need to avoid a program size explosion caused by reflection support for many, many declarations in a situation where only a handful of them would actually be needed. This means that missing capabilities is a delicate and typical issue: You need to choose the included capabilities carefully, and also to annotate classes carefully, in order to avoid having this program size problem. This might not be important in this particular situation, but reflectable is still built to save space by only supporting the features that are requested (by including the corresponding capabilities in the given reflector class declaration).

In this particular case, there was no capability to invoke a getter named name on a given InstanceMirror (if it's an instance getter), a ClassMirror (if it is a static getter), or LibraryMirror (if it is a top-level getter). I'm not sure how that happened (because invokingCapability should cover all kinds of invocations), but please try to fix the other things and then return if this issue persists.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文