2
我正在尝试在JScript.NET中使用BeginRead()执行异步IO,但无法使回调函数正常工作。强制转换委托类型在JScript.NET中失败
下面是代码:
function readFileAsync() {
var fs : FileStream = new FileStream('test.txt', FileMode.Open, FileAccess.Read);
var result : IAsyncResult = fs.BeginRead(new byte[8], 0, 8, readFileCallback), fs);
Thread.Sleep(Timeout.Infinite);
}
var readFileCallback = function(result : IAsyncResult) : void {
print('ListenerCallback():');
}
唯一的例外是铸造失败:
Unhandled Exception: System.InvalidCastException: Unable to cast object of type 'Microsoft.JScript.Closure' to type 'System.AsyncCallback'.
at JScript 0.readFileAsync(Object this, VsaEngine vsa Engine)
at JScript 0.Global Code()
at JScript Main.Main(String[])
我试图做一个明确的转换既AsyncCallback的和基本MulticastDelegate和委托类型没有无济于事。
代表都应该是自动生成,避免需要明确地创建一个新的AsyncCallback,如:
BeginRead(... new AsyncDelegate(readFileCallback), object);
而事实上,如果你尝试创建委托明确,编译器会发出错误。我必须在这里错过一些东西。