Dart:从字符串创建方法
Dart: Create method from string
我一直在尝试使用 Dart 从字符串动态创建方法,但无济于事。字符串示例:“(String str) => return str.length;”。这个想法是允许用户创建自己的函数以应用于给定的字符串。我发现的唯一一件事是 NoSuchMethod ,它似乎不适用于我的情况。我尝试在 JavaScript 中使用新函数,但是当将函数传递给 Dart 并执行它时,出现以下错误:Uncaught TypeError: J.$index$asx(.. .).call$0 不是函数.
代码示例:
飞镖:
context["UpdateNames"] =
(JsObject pTag)
{
print(pTag["function"]("text"));
};
JS:
function execute ()
{
var func = {"function": new Function("str", "return str.length;")};
UpdateNames(func);
}
编辑:
解决方案:在 JavaScript 中创建一个对象,例如:
this.fun = function (name)
{
var text = "var funs = " + document.getElementById("personalFun").value;
eval(text);
return funs(name);
};
然后在Dart中创建对象:
caller = new JsObject(context['Point'], []);
最后调用动态创建函数的方法:
caller.callMethod('fun', [text]);
我不确定是否完全理解您想要实现的目标,因此我会尽力提供最佳答案
您想将方法动态添加到具有特定字符串标识符的 class
在这种情况下,这是完全可行的,但您需要使用一些镜像,因此如果您想将其用于网络,请小心
这里是一个实现示例:
import "dart:mirrors";
class Test {
Map<String, dynamic> _methods = {};
void addMethod(String name, var cb) {
_methods[name] = cb;
}
void noSuchMethod(Invocation inv) {
if (inv.isMethod) {
Function.apply(_methods[MirrorSystem.getName(inv.memberName)], inv.positionalArguments);
}
}
}
void testFunction() {
for (int i = 0; i < 5; i++) {
print('hello ${i + 1}');
}
}
void testFunctionWithParam(var n) {
for (int i = 0; i < 5; i++) {
print('hello ${i + n}');
}
}
void main() {
var t = new Test();
t.addMethod("printHello", testFunction);
t.addMethod("printHelloPlusN", testFunctionWithParam);
t.printHello();
t.printHelloPlusN(42);
}
您想在字符串中“执行”代码
抱歉,这是不可能的。这是一个很大的要求功能,但飞镖团队没有计划,因为它会涉及许多变化和权衡。
也许可以通过创建 dart 文件并使用 isolate 到 运行 来进行欺骗。
解决方案:在 JavaScript 中创建一个对象,例如:
var FunctionObject = function() {
this.fun = function (name)
{
var text = "var funs = " + document.getElementById("personalFun").value;
eval(text);
return funs(name);
};
};
然后在Dart中创建对象:
caller = new JsObject(context['FunctionObject'], []);
最后调用动态创建函数的方法:
caller.callMethod('fun', [text]);
我一直在尝试使用 Dart 从字符串动态创建方法,但无济于事。字符串示例:“(String str) => return str.length;”。这个想法是允许用户创建自己的函数以应用于给定的字符串。我发现的唯一一件事是 NoSuchMethod ,它似乎不适用于我的情况。我尝试在 JavaScript 中使用新函数,但是当将函数传递给 Dart 并执行它时,出现以下错误:Uncaught TypeError: J.$index$asx(.. .).call$0 不是函数.
代码示例:
飞镖:
context["UpdateNames"] =
(JsObject pTag)
{
print(pTag["function"]("text"));
};
JS:
function execute ()
{
var func = {"function": new Function("str", "return str.length;")};
UpdateNames(func);
}
编辑:
解决方案:在 JavaScript 中创建一个对象,例如:
this.fun = function (name)
{
var text = "var funs = " + document.getElementById("personalFun").value;
eval(text);
return funs(name);
};
然后在Dart中创建对象:
caller = new JsObject(context['Point'], []);
最后调用动态创建函数的方法:
caller.callMethod('fun', [text]);
我不确定是否完全理解您想要实现的目标,因此我会尽力提供最佳答案
您想将方法动态添加到具有特定字符串标识符的 class
在这种情况下,这是完全可行的,但您需要使用一些镜像,因此如果您想将其用于网络,请小心
这里是一个实现示例:
import "dart:mirrors";
class Test {
Map<String, dynamic> _methods = {};
void addMethod(String name, var cb) {
_methods[name] = cb;
}
void noSuchMethod(Invocation inv) {
if (inv.isMethod) {
Function.apply(_methods[MirrorSystem.getName(inv.memberName)], inv.positionalArguments);
}
}
}
void testFunction() {
for (int i = 0; i < 5; i++) {
print('hello ${i + 1}');
}
}
void testFunctionWithParam(var n) {
for (int i = 0; i < 5; i++) {
print('hello ${i + n}');
}
}
void main() {
var t = new Test();
t.addMethod("printHello", testFunction);
t.addMethod("printHelloPlusN", testFunctionWithParam);
t.printHello();
t.printHelloPlusN(42);
}
您想在字符串中“执行”代码
抱歉,这是不可能的。这是一个很大的要求功能,但飞镖团队没有计划,因为它会涉及许多变化和权衡。
也许可以通过创建 dart 文件并使用 isolate 到 运行 来进行欺骗。
解决方案:在 JavaScript 中创建一个对象,例如:
var FunctionObject = function() {
this.fun = function (name)
{
var text = "var funs = " + document.getElementById("personalFun").value;
eval(text);
return funs(name);
};
};
然后在Dart中创建对象:
caller = new JsObject(context['FunctionObject'], []);
最后调用动态创建函数的方法:
caller.callMethod('fun', [text]);