借助 edge.js 在 node.js 模块中使用 C# 调用方法
calling method with C# in node.js module by the help of edge.js
我正在尝试使用 edge.js 在 C# 中使用 node.js 模块。
我可以访问模块的 属性,但不能访问方法。
using System;
using System.Threading.Tasks;
using EdgeJs;
class Program
{
static void Main(string[] args)
{
Start().Wait();
}
public static async Task Start()
{
var func = Edge.Func(@"return require('../require.js')");
var invoke = func.Invoke(new { });
var require = (dynamic)invoke.Result;
Console.WriteLine(require.answer1); // answer 1
var deleg = require.answer2;
Console.WriteLine(deleg);
var task = deleg.Invoke(1);
Console.WriteLine(task);
// var result = await task.Run(); // error
// Console.WriteLine(result);
}
}
require.js
module.exports = function (data, callback) {
var module = require('./module');
var result = module;
callback(null, result);
};
module.js
exports.answer1 = "answer 1";
exports.answer2 = function () {
return "answer 2";
};
有没有办法从 C# 调用 node.js 中的函数并访问 "answer 2"?
此处错误:
answer 1
System.Func`2[System.Object,System.Threading.Tasks.Task`1[System.Object]]
System.Threading.Tasks.Task`1[System.Object]
Unhandled Exception: System.AggregateException: One or more errors occurred. ---
> Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: No overload for method
'Run' takes '0' arguments
at CallSite.Target(Closure , CallSite , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T
0 arg0)
at Program.<Start>d__d.MoveNext() in c:\Users\admin\Documents\Visual Studio 2
013\Projects\snippets\snippets\Program.cs:line 26
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceled
Exceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationTo
ken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at Program.Main(String[] args) in c:\Users\admin\Documents\Visual Studio 2013
\Projects\snippets\snippets\Program.cs:line 10
Press any key to continue . . .
我认为您需要将代码调整为以下内容:
var deleg = require.answer2;
Console.WriteLine(deleg);
var task = (Func<object,Task<object>>)deleg;
Console.WriteLine(task);
var result = await task(1);
Console.WriteLine(result);
我直接从 github 自述文件中获取了这个,但本质上调用函数调用实际上并不像普通同步委托那样工作,相反它只是 returns 立即控制但是不提供实际上可以 运行 任务的调度程序。
答案是将 module.js 更改为:
exports.answer2 = function (input, cb) {
cb(null, "answer 2");
};
我从edge.js
那里得到了答案
我正在尝试使用 edge.js 在 C# 中使用 node.js 模块。
我可以访问模块的 属性,但不能访问方法。
using System;
using System.Threading.Tasks;
using EdgeJs;
class Program
{
static void Main(string[] args)
{
Start().Wait();
}
public static async Task Start()
{
var func = Edge.Func(@"return require('../require.js')");
var invoke = func.Invoke(new { });
var require = (dynamic)invoke.Result;
Console.WriteLine(require.answer1); // answer 1
var deleg = require.answer2;
Console.WriteLine(deleg);
var task = deleg.Invoke(1);
Console.WriteLine(task);
// var result = await task.Run(); // error
// Console.WriteLine(result);
}
}
require.js
module.exports = function (data, callback) {
var module = require('./module');
var result = module;
callback(null, result);
};
module.js
exports.answer1 = "answer 1";
exports.answer2 = function () {
return "answer 2";
};
有没有办法从 C# 调用 node.js 中的函数并访问 "answer 2"?
此处错误:
answer 1
System.Func`2[System.Object,System.Threading.Tasks.Task`1[System.Object]]
System.Threading.Tasks.Task`1[System.Object]
Unhandled Exception: System.AggregateException: One or more errors occurred. ---
> Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: No overload for method
'Run' takes '0' arguments
at CallSite.Target(Closure , CallSite , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T
0 arg0)
at Program.<Start>d__d.MoveNext() in c:\Users\admin\Documents\Visual Studio 2
013\Projects\snippets\snippets\Program.cs:line 26
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceled
Exceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationTo
ken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at Program.Main(String[] args) in c:\Users\admin\Documents\Visual Studio 2013
\Projects\snippets\snippets\Program.cs:line 10
Press any key to continue . . .
我认为您需要将代码调整为以下内容:
var deleg = require.answer2;
Console.WriteLine(deleg);
var task = (Func<object,Task<object>>)deleg;
Console.WriteLine(task);
var result = await task(1);
Console.WriteLine(result);
我直接从 github 自述文件中获取了这个,但本质上调用函数调用实际上并不像普通同步委托那样工作,相反它只是 returns 立即控制但是不提供实际上可以 运行 任务的调度程序。
答案是将 module.js 更改为:
exports.answer2 = function (input, cb) {
cb(null, "answer 2");
};
我从edge.js
那里得到了答案