Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#调用COM API会导致类型不匹配。(HRESULT的异常:0x80020005(显示类型不匹配))_C#_.net - Fatal编程技术网

C#调用COM API会导致类型不匹配。(HRESULT的异常:0x80020005(显示类型不匹配))

C#调用COM API会导致类型不匹配。(HRESULT的异常:0x80020005(显示类型不匹配)),c#,.net,C#,.net,我正在将一个旧的vba应用程序升级为C#应用程序。该应用程序自动化Solid Edge(西门子的cad 3D程序)。调用Solid Edge API的特定方法时遇到问题。所讨论的方法有一组ref和out参数,我只关心其中一个,表示当前Solid Edge绘图的质量。我决定包装这个方法并有一个返回值 public unsafe double GetMass() { //object arr = System.Reflection.Missing.Value; Array Arr =

我正在将一个旧的vba应用程序升级为C#应用程序。该应用程序自动化Solid Edge(西门子的cad 3D程序)。调用Solid Edge API的特定方法时遇到问题。所讨论的方法有一组ref和out参数,我只关心其中一个,表示当前Solid Edge绘图的质量。我决定包装这个方法并有一个返回值

public unsafe double GetMass()
{
    //object arr = System.Reflection.Missing.Value;
    Array Arr = null;
    //The interface doesn't let us just get phm. Thats why we need 1 assignable of each type to satisfy the interface and get our out value.
    AssemblyDocument.PhysicalProperties.GetAssemblyPhysicalProperties(
        out double Mass, 
        out double AssignableDouble,
        out double AssignableDouble2,
        ref Arr,
        ref Arr,
        ref Arr,
        ref Arr,
        ref Arr,
        ref Arr,
        ref Arr,
        ref Arr,
        out bool Bool1,
        out bool Bool2
        );
    return Mass;
}
我也试着让它成为一种不安全的方法,正如你所看到的,只是在这一点上反复尝试。调用GetAssemblyPhysicalProperties返回错误:

Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))
以下是文档中的方法签名:

在旧的vba应用程序中,我们使用除两个布尔外的所有double调用该方法。在C#元数据中,方法签名已更改为:

void GetAssemblyPhysicalProperties(out double Mass, out double Volume, out double Area, ref Array CenterOfMass, ref Array CenterOfVolume, ref Array GlobalMoments, ref Array PrincipalAxis1, ref Array PrincipalAxis2, ref Array PrincipalAxis3, ref Array PrincipalMoments, ref Array RadiiOfGyration, out bool IsSick, out bool UpdateStatus);
查看文档,数组中放置了多个坐标。在我们的vba代码中,我们以数组的形式传递双精度

问题: 1.为什么这个调用在vba中都是双精度的而不是数组中工作?为什么它不会导致错误? 2.我怎样才能使这个代码工作?我尝试过传递不同的类型,但它不允许我用数组类型编译其他任何方式。我也在Solid Edge和这个特定错误上搜索了谷歌2个页面。在大多数情况下,它们与ref数组参数无关。再一次,我只关心得到群众的支持

编辑: 感谢Hans Passant,我现在拥有以下工作代码:

public double GetMass()
        {
            //object arr = System.Reflection.Missing.Value;
            Array Arr = new double[0];
            //The interface doesn't let us just get phm. Thats why we need 1 assignable of each type to satisfy the interface and get our out value.
            AssemblyDocument.PhysicalProperties.GetAssemblyPhysicalProperties(
                out double Mass, 
                out double AssignableDouble,
                out double AssignableDouble2,
                ref Arr,
                ref Arr,
                ref Arr,
                ref Arr,
                ref Arr,
                ref Arr,
                ref Arr,
                ref Arr,
                out bool Bool1,
                out bool Bool2
                );
            return Mass;
        }

使用double[],不要为所有参数传递相同的变量。@使用双数组的HansPassant不允许我编译。->参数4:无法从“ref double[]”转换为“ref System.Array”使用数组类型的变量,请使用新的double[0]对其进行初始化。如果您使用C#v4及以上版本,那么就不必再使用ref,这可能是一个解决方法。一定要考虑接触SelddGE,我想他们有一个包装纸,有点不那么格栅。非常感谢。如果您发布了答案,我会将其标记为正确的internet点。请使用double[],不要为所有参数传递相同的变量。@使用double数组的HansPassant不允许我编译。->参数4:无法从“ref double[]”转换为“ref System.Array”使用数组类型的变量,请使用新的double[0]对其进行初始化。如果您使用C#v4及以上版本,那么就不必再使用ref,这可能是一个解决方法。一定要考虑接触SelddGE,我想他们有一个包装纸,有点不那么格栅。非常感谢。如果你发布一个答案,我会为他们标记为正确的甜蜜互联网点。