“参数”的格式是什么,参数到ClassMirror.newInstance()? [英] What is the format of the "arguments" parameter to ClassMirror.newInstance()?

查看:135
本文介绍了“参数”的格式是什么,参数到ClassMirror.newInstance()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全愿意玩这个,直到我得到它,但希望有人可能给我一个提示。该参数在docs(gen-dartdocs / dart-mirrors / ClassMirror / newInstance.html)中声明为

I'm perfectly willing to play with this until I get it right, but was hoping someone might give me a hint. The parameter is declared in the docs (gen-dartdocs/dart-mirrors/ClassMirror/newInstance.html) as

InstanceMirror newInstance(Symbol constructorName,
                      List positionalArguments,
                      [Map<Symbol,dynamic> namedArguments]);

在文档中有对positionsArguments和namedArguments格式的一个很好的写法。

There is a nice writeup on the format of positionalArguments and namedArguments in the docs. However, it is just a little on the abstract side of my current tolerance level.

一个体面的讨论也存在于
http://japhr.blogspot.com/2014/06/dart-factory-method-pattern.html

A decent discussion also exists at http://japhr.blogspot.com/2014/06/dart-factory-method-pattern.html But, alas, no examples of actually passing args into the method.

在我的例子中,我只想传递两个args,

In my case, I would like to simply pass two args, "title" and "description" into an unnamed subclass constructor.

到目前为止,我的代码是:

Here's my code so far:

:item.dart

file: item.dart

import 'dart:mirrors';

abstract class Item {

    String title;
    String description;

    factory Item(String type) {
      MirrorSystem libs = currentMirrorSystem();
      LibraryMirror lib = libs.findLibrary(new Symbol('app.models'));
      Map<Symbol, Mirror> classes = lib.declarations;
      // To do: handle exception if class not found
      ClassMirror cls = classes[new Symbol(type)];
      // TODO:
      //  verify each subclass has no-arg ctor
      //  determ how to pass args to ctor.
      InstanceMirror inst = cls.newInstance(new Symbol(''), []);
      return inst.reflectee;
    }

    // conflicts w/ Item factory
//  Item(this.title, this.description);
}

下面是实例化的类:

file:model.dart

file: model.dart

library app.models;

import 'item.dart' show Item;

/// The barebones model for a codelab. Defines constants used for validation.
class Codelab implements Item {
   // ...
}

最后,这里是如何调用Item工厂。 ItemElement是其自己的层次结构的超类,由CodelabElement子类化:

Finally, here is how the Item factory is called. ItemElement is the superclass of its own hierarchy, subclassed by CodelabElement:

文件:item_element.dart:

file: item_element.dart:

import 'item.dart' show Item;

class ItemElement {
    Item item;
    final String itemType;

    ItemElement() {
      item = new Item(itemType);
    }
    // ...
}

和CodelabElement :

And CodelabElement:

档案:codelab_element.dart

file: codelab_element.dart

import 'model.dart' show Codelab;
import 'item_element.dart' show ItemElement;

class CodelabElement extends ItemElement {

    final itemType = "Codelab";

    CodelabElement() : super() {}

    //...
}

然后:

文件:main.dart

file: main.dart

void main() {
    var element = new CodelabElement();
}

目前,新的Codelab实例从newInstance ,但它不包含继承的'title'和'description'attrs。

Currently, the new Codelab instance is returned from newInstance() (very cool), but it doesn't contain the inherited 'title' and 'description' attrs.

也许它与我不清楚extend的用法有关系,和implements。

Maybe it has something to do with my being unclear on the usage of "extends" and "implements".

推荐答案

这应该起作用

cls.newInstance(new Symbol(''), ['a', 1] /*, 
    {#arg1Name: 'arg1Value', #arg2Name: 'arg2Value'}*/ );

并且像

new MyClass('a', 1, arg1Name: 'arg1Value' /*, arg2Name: 'arg2Value'*/); 

只是看到了Named个参数没有实现。

Just saw, Named arguments are not implemented.

您可以在 DartPad 中尝试。

这篇关于“参数”的格式是什么,参数到ClassMirror.newInstance()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆