简体   繁体   English

System.Object[*] C# 的维度

[英]Dimension of System.Object[*] C#

I am converting an Excel VSTO addin from VB.NET to C#.我正在将 Excel VSTO 插件从 VB.NET 转换为 C#。 I am struck with getting the dimension of the System.object[*] returned by the Application.Evaluate() function.我对 Application.Evaluate() function 返回的 System.object[*] 的维度感到震惊。

I get the error message below during the runtime when the function is called.在调用 function 时,我在运行时收到以下错误消息。

Unable to cast object of type System.Object[*] to System.Object[].无法将 System.Object[*] 类型的 object 转换为 System.Object[]。

Below are the original VB.NET code followed by the C# code.下面是原始 VB.NET 代码,后跟 C# 代码。 I had no issues with VB.NET code.我对 VB.NET 代码没有任何问题。

I tried to cast to Array, dynamic but of no use.我尝试强制转换为动态但没用的数组。 Is there any alternative way to achieve my goal of getting the dimension of the Excel Application.Evaluate() function?有没有其他方法可以实现我获得 Excel Application.Evaluate() function 尺寸的目标?

If GetDimension(xlapp.Application.Evaluate(FormulaString)) = 0 Then
    'The formula results in single value.
Else
     'The formula is Matrix Based. 
End If

Below is original VB.NET Sub function.下面是原装 VB.NET 子 function。

    Private Function GetDimension(MatResult As Object) As Integer
        Dim i As Integer
        Dim LoopCheck As Boolean = True
        Dim RankException As Integer
        Dim Dimension As Integer
        i = 0
        Do
          i += 1
          Try
              RankException = UBound(MatResult, i)
          Catch ex As Exception
              Dimension = i - 1
              LoopCheck = False
          End Try

       Loop Until Not LoopCheck

       Return Dimension 
   End Function

Below is the converted code in C#下面是 C# 中的转换代码

if(GetDimension(xlapp.Application.Evaluate(FormulaString)) == 0)
    //The formula results in single value.
else
     //The formula is Matrix Based. 

The converted function is below.转换后的 function 如下。

    public int GetDimension(object MatResult)
    {
        int i;
        bool LoopCheck = true;
        int RankException;

        if (MatResult.GetType() == typeof(double) || MatResult.GetType() == typeof(string))
            return 0;

        int Dimension = 0;
        Array CastedMat = (Array)MatResult;
        i = 0;
        do
        {
            i++;
            try
            {
                RankException = CastedMat.GetLength(i);
            }
            catch (Exception ex)
            {
                Dimension = i - 1;
                LoopCheck = false;
            }
        }
        while (LoopCheck);

        return Dimension;
    }

I am able to resolve this issue through some hints provided above.我可以通过上面提供的一些提示来解决这个问题。 Below is the code which is working.下面是正在运行的代码。

    public int GetDimension(object MatResult)
    {
         int MatrixRank;

        if (MatResult.GetType() == typeof(double) || MatResult.GetType() == typeof(string))
            return 0;

        Array CastedMat = MatResult as Array;
        MatrixRank = CastedMat.Rank;
        return MatrixRank;
    }

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

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