如何创建我们自己的飞镖元数据? [英] How to create our own metadata in Dart?

查看:190
本文介绍了如何创建我们自己的飞镖元数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创造一些自己的元数据为我的飞镖$ C $厘米,例如@table,@Column,但我无法找到任何有用的文档了。

I want to create some own metadata for my dart codem, e.g. @table, @column, but I can't find any useful documents about it.

不过,我发现,目前有在angular.dart一些特殊的元数据(例如NgController):<一href=\"https://github.com/angular/angular.dart/blob/master/demo/todo/web/todo.dart#L52\">https://github.com/angular/angular.dart/blob/master/demo/todo/web/todo.dart#L52

But I do found there are some special metadata (e.g. NgController) in angular.dart: https://github.com/angular/angular.dart/blob/master/demo/todo/web/todo.dart#L52

如何创建达特我自己的元数据?有没有什么文件?

How to create my own metadata in Dart? Is there any documents?

推荐答案

DART支持其用于连接用户定义的注释的程序结构的元数据。

Dart supports metadata which is used to attach user defined annotations to program structures.

元数据包括一系列的注释,其中每一个开始与字符@的,随后的恒定前pression与标识符开始。这是一个编译时错误,如果前pression不是下列之一:

Metadata consists of a series of annotations, each of which begin with the character @, followed a constant expression that starts with an identifier. It is a compile time error if the expression is not one of the following:


  1. 要编译时间常数变量的引用。

  2. 要恒定的构造函数的调用。

元数据可以出库,部分头,类别,类型定义,类型参数,构造,工厂,函数,字段,参数或变量声明之前和进口,出口或部分指令之前出现。

Metadata can appear before a library, part header, class, typedef, type parameter, constructor, factory, function, field, parameter, or variable declaration and before an import, export or part directive.

因此​​,通过你的常量建议这样 @table @column 按功能是非常有限的,因为他们不能持有附加信息(参数)。

So, suggested by you constants such @table, @column are very limited by functionality because they cannot hold additional information (parameters).

@DataTable("sale_orders")
class SaleOrder {
  @DataColumn("sale_order_date")
  DateTime date;
}

@table
class Product {
  @column
  String name;
}

const DataColumn column = const DataColumn();

const DataTable table = const DataTable();

class DataTable {
  final String name;

  const DataTable([this.name]);
}

class DataColumn {
  final String name;

  const DataColumn([this.name]);
}

但在任何情况下,您选择最适合您需要的选项。

But in any case, you choose the option that best suits your needs.

这篇关于如何创建我们自己的飞镖元数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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