【问题标题】:Dart: how to get the "Type" from the classDart:如何从类中获取“类型”
【发布时间】:2012-11-07 15:26:33
【问题描述】:

如何获取给定类的“类型”实例?

基于http://www.dartlang.org/articles/m1-language-changes/#first-class-types,例如这样的构造:

class Foo{}

Type type = Foo;

似乎可以通过分析器,但在 Dartium 中运行时会出错。这只是 VM 中尚不支持,还是语法错误?或者我是否需要以某种方式注释 Foo,以便在运行时保留类型信息,或者其他什么?

谢谢

理查德。

【问题讨论】:

    标签: dart


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      现在您可以使用https://api.dartlang.org/stable/2.3.1/dart-core/Object/runtimeType.html

      class Foo{}
      var foo = Foo();
      
      assert(foo.runtimeType == Foo);
      

      【讨论】:

        猜你喜欢
        • 2021-07-02
        • 2021-05-19
        • 2022-06-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多