dart:代理注解用法

标签 dart proxy-classes dart-editor dart-analyzer

@proxy 注释的文档说明:

If a class is annotated with @proxy, or it implements any class that is annotated, then the class is considered to implement any interface and any member with regard to static type analysis. As such, it is not a static type warning to assign the object to a variable of any type, and it is not a static type warning to access any member of the object.

但是,给定以下代码:

import 'dart:mirrors';

@proxy
class ObjectProxy{
  final InstanceMirror _mirror;

  ObjectProxy(Object o): _mirror = reflect(o);

  @override
  noSuchMethod(Invocation invocation){
    print('entered ${invocation.memberName}');
    var r = _mirror.delegate(invocation);
    print('returning from ${invocation.memberName} with $r');
    return r;
  }
}

class ClassA{
  int k;

  ClassA(this.k);
}

void printK(ClassA a) => print(a.k);

main() {
  ClassA a = new ObjectProxy(new ClassA(1)); //annoying
  printK(a);
}

dart 编辑器警告

A value of type 'ObjectProxy' cannot be assigned to a variable of type 'ClassA'.

代码在未检查模式下按预期执行,但警告很烦人,据我所知,抑制该警告是 @proxy 标记唯一应该做的事情。

我是否误解了 @proxy 标签的用法,或者这是 dart 编辑器/分析器的错误?

最佳答案

你能做的是

@proxy
class ObjectProxy implements ClassA {
  final InstanceMirror _mirror;

  ObjectProxy(Object o): _mirror = reflect(o);

  @override
  noSuchMethod(Invocation invocation){
    print('entered ${invocation.memberName}');
    var r = _mirror.delegate(invocation);
    print('returning from ${invocation.memberName} with $r');
    return r;
  }
}

您需要声明 ObjectProxy 实现了一个接口(interface),但您不必实际实现它。 如果您认为这是一个错误报告,请访问 http://dartbug.com

关于dart:代理注解用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29135912/

相关文章:

dart - 用pub解决依赖关系失败

dart - 如何检查 dart 中的类型错误

datetime - 在Flutter获得凌晨0点的简单方法是什么?

angularjs - Angular 达特 : How to use two ng-repeat expressions with one tag?

flutter - 当我尝试使用提供程序从 ListView 中删除项目时,它在 flutter 中从列表中删除了最后一个元素

java - 返回 List<WebElements> 周围的代理

flutter - 如何在 Flutter 中为小部件添加边框?

java - 使用泛型进行代理

java - 指定正确的 spring-instrument agent jar 的最佳实践

dart - 如何告诉 dart 编辑器显示项目文件夹中的所有文件?