简体   繁体   English

C#委托Invalidcast异常

[英]C# delegate Invalidcast Exception

I am stuck with an InvalidCast Exception. 我陷入了InvalidCast异常。 I am calling a delegate to run some function. 我正在呼叫代表以执行某些功能。 In the callback method of the delegate I am trying to get the return value of the function as shown below. 在委托的回调方法中,我尝试获取函数的返回值,如下所示。

     public delegate SyncHelper.SyncPlan RunJobDelegate();

     public static void SyncJobCallback(IAsyncResult result)
    {

        RunJobDelegate runSyncJob = (RunJobDelegate)result.AsyncState;
        SyncHelper.SyncPlan obj_Plan = runSyncJob.EndInvoke(result);

When RunJobDelegate runSyncJob = (RunJobDelegate)result.AsyncState; 当RunJobDelegate runSyncJob =(RunJobDelegate)result.AsyncState; is called I am hitting an exception saying "Unable to cast object of type 'RunJobDelegate' to type 'RunJobDelegate'. " 被称为我遇到一个异常,说“无法将类型为'RunJobDelegate'的对象转换为类型为'RunJobDelegate'。”

Please help me with the solution if anyone has seen this before. 如果有人以前见过,请为我提供解决方案。

Divya. 迪夫亚。

This sort of thing should only ocurr if you have two different RunJobDelegate types defined in your code. 仅当您在代码中定义了两种不同的RunJobDelegate类型时,才应该发生这种情况。 Make sure it isn't defined twice. 确保没有定义两次。

If that isn't the problem, please most more of the code (including the calling function) for us to examine. 如果这不是问题,请让我们检查更多的代码(包括调用函数)。

Did you check the type of result.AsyncState in the debugger? 您是否在调试器中检查了result.AsyncState的类型?

also are you sure you want to use AsyncState and not AsyncDelegate? 您还确定要使用AsyncState而不是AsyncDelegate吗?

Reference: http://msdn.microsoft.com/en-us/library/system.iasyncresult.asyncstate.aspx 参考: http : //msdn.microsoft.com/en-us/library/system.iasyncresult.asyncstate.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM