使用C#调用COM+对象,通过里面的方法返回的对象一律都是System.__ComObject类型,如果要对该对象进行操作,必须转换为实际类型才行,但问题是我不知道其实际类型是什么,有什么方法可以知道System.__ComObject的实际类型呢?

解决方案 »

  1.   

    是通过“Name”获取的吧?
    object[,] obj =(object[,])xlsName.RefersToRange.Value2;
    string str=obj[1,1].ToString();
      

  2.   

    object d = m_BillTransfer.get_BillForm();
    比如如上代码,获取的d的类型就是System.__ComObject,如何知道其实际类型是什么?
      

  3.   


    类似的  找到你需要取值的范围Range   然后用上面的代码    
    你读取的是什么文件?
      

  4.   

    引自 微软 如何: 检查 COM 对象 (System.__ComObject) 使用 Visual C#.net 的类型
    http://support.microsoft.com/kb/320523/zh-cn在 Microsoft.net 框架中的 COM 对象 GetType.Type 运算符将返回 System.__ComObject 类。在某些编码方案中,您可能不得不知道特定类的对象。例如对于 Microsoft excel 的自动化客户端可能需要确定用户在 Excel 中所选对象的类型。在 Microsoft Visual C#.net,as 运算符类似于一个 强制转换 运算符只是产生空转换失败,而不是引发异常。as 运算符用于将 COM 对象与特定类型进行比较。如果强制转换没有返回空值,则转换是成功的。下面的分步过程演示如何使用 as 运算符使用 Visual C#.net 来检查 COM 对象的类型。
     1. 在 Visual C#.net 中创建新的 Windows 应用程序项目。默认状态下,创建 Form1。
       2. 添加到 Microsoft Excel 对象库 的引用。若要这样做,请按照下列步骤操作:
             1. 在 项目 菜单上单击 添加引用。
             2. 添加引用 对话框中单击 COM 选项卡,单击 Microsoft Excel 对象库,然后单击 选择。            注意Microsoft Office 2003 包括主互操作程序集 (pia)。Microsoft Office XP 不包括 pia,但它们可能会被下载。 有关 Office XP pia 的其他信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:
                328912  (http://support.microsoft.com/kb/328912/ ) 信息: Microsoft Office XP pia 可用于下载
             3. 若要接受您的选择,单击 确定。
       3. 在 视图 菜单上选择 工具箱,然后向 Form1 添加一个按钮和文本框。
       4. 若要显示在窗体的代码窗口,双击 Form1。             与下列:
          Excel.ApplicationClass oExcel;
          Excel.Workbooks oBooks;
          Excel.Workbook oBook;      private void Form1_Load(object sender, System.EventArgs e)
          {
           //Start Microsoft Excel with a new workbook and give control
           //to the user.
           oExcel = new Excel.ApplicationClass();
           oBooks = oExcel.Workbooks;
           oBook = oBooks.Add(System.Reflection.Missing.Value);
           oExcel.Visible=true;
           oExcel.UserControl=true;       //Layout the controls on Form1 and set up the Click event 
           //handler for the button.
           textBox1.Text ="";
           textBox1.Width = 200;
           button1.Text = "Get Selection";
           this.button1.Click += new System.EventHandler(this.ButtonClick);
          }      private void ButtonClick(object sender, System.EventArgs e)
          {
           object o = oExcel.Selection;       //Display a message about the selection type in Excel.
           if((o as Excel.Range)!=null)
           {
           textBox1.Text = "Selection is Excel.Range";
           }
           else if((o as Excel.Rectangle)!=null)
           {
           textBox1.Text = "Selection is Excel.Rectangle";
           }
           else if((o as Excel.ChartObject)!=null)
           {
           textBox1.Text = "Selection is Excel.ChartObject";
           }
           else if((o as Excel.ChartArea)!=null)
           {
           textBox1.Text = "Selection is Excel.ChartArea";
           }
           // ... There are many additional Selection types you could check for if needed ...
           else
           {
           textBox1.Text = "Selection is Unknown";
           }      }
              //6. 将下面的 USING 语句添加到代码模块:      using Excel = Microsoft.Office.Interop.Excel;