1

我有一个在 C# .NET 4.0 应用程序中使用的 COM API。

代码如下所示:

if (!object.ReferenceEquals((comObject.Value), null))
{
   // Do something
}

where comObjectis of typeSystem.__ComObjectValueis of type dynamic

一般来说,这可以正常工作,但是当实际类型Value是字节数组时,它会抛出以下InvalidCastException异常:

   Unable to cast object of type 'System.Byte[*]' to type 'System.Byte[]'.

   at CallSite.Target(Closure , CallSite , Type , Object , Object )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
   at ReMetricaTypeSpike.Program.Main(String[] args) in c:\my documents\visual studio 2010\Projects\ReMetricaTypeSpike\ReMetricaTypeSpike\Program.cs:line 28
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

该线程http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/24c0bd62-058a-487e-91ab-ed1bb0b0cca4似乎表明抛出异常是因为数组是基于 1 的(它是 -我认为 API 是用 VB 编写的)。但是,我从不明确地进行这种转换,这一切都由动态自动代码处理。

这是 C# 动态的已知错误吗?还是有其他事情发生?

4

0 回答 0