Cast to delegate type fails in JScript.NET
Posted
by dnewcome
on Stack Overflow
See other posts from Stack Overflow
or by dnewcome
Published on 2010-05-02T02:00:53Z
Indexed on
2010/05/02
2:07 UTC
Read the original article
Hit count: 449
jscript.net
|.NET
I am trying to do async IO using BeginRead() in JScript.NET, but I can't get the callback function to work correctly.
Here is the code:
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():' );
}
The exception is a cast failure:
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[] )
I have tried doing an explicit cast both to AsyncCallback and to the base MulticastDelegate and Delegate types to no avail.
Delegates are supposed to be created automatically, obviating the need for creating a new AsyncCallback explicitly, eg:
BeginRead( ... new AsyncDelegate( readFileCallback), object );
And in fact if you try to create the delegate explicitly the compiler issues an error. I must be missing something here.
© Stack Overflow or respective owner