C#系统.__ ComObject访问的Excel文件时返回 [英] C# System.__ComObject returned when accessing Excel file

查看:887
本文介绍了C#系统.__ ComObject访问的Excel文件时返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过下面的代码来访问Excel文件:

I am trying to access Excel file by code below:

Microsoft.Office.Interop.Excel.Application ObjExcel
  = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook book;
Microsoft.Office.Interop.Excel.Worksheet sheet;
Excel.Range range1 = null, range2 = null;
CultureInfo ci = new CultureInfo("en-US");
Thread thisThread = Thread.CurrentThread;
thisThread.CurrentCulture = new CultureInfo("en-US");
book = ObjExcel.Workbooks.Open(LinguisticInstructionsFileName);



在书变量的最后一行含量系统.__ ComObject ,而在不同的应用程序代码工作和变量 ... Excel.WorkbookClass

At last line content of book variable is System.__ComObject while in different app code is working and variable has ...Excel.WorkbookClass.

所以我想问什么可能是这种奇怪的行为的原因。我已经试着拨打Workbooks.Open额外Missing.Value ARGS,但结果是一样的。

So I would like to ask what might be the reason for this strange behavior. I already tried to call Workbooks.Open with extra Missing.Value args but result was the same.

推荐答案

.NET COM实现使用代理对象,这些代理是内部产生,并从系统.__ ComObject派生所以这是一个正常的行为。
他们是一种动态对象的。
你可以施放__ComObject到接口或者你需要的类。
你可以将它转换为WorkbookClass没有问题。

.NET COM implementation uses proxy objects, these proxies are generated internally and derive from System.__ComObject so that's a normal behaviour. They are a sort of "dynamic objects". You can cast the __ComObject to the interface or the class you need. You can cast it to WorkbookClass without problems.

这一切都取决于你创建新的对象如何获取实例......,例如,新WorkbookClass ,C#将直接创建强类型对象。
相反,如果对象是从COM功能或经常返回__ComObject属性,这是因为__ComObject可几乎所有的东西回来,可强制转换为多个接口,当然C#不知道该怎么回这样它返回这个特殊的动态的对象。这些对象被称作RCW一种代表运行时可调用包装。

It all depends on how you get the instance... if you create an object with new, for example, new WorkbookClass, C# will directly create the strong typed object. Instead if the object is returned from a COM function or a property it returns often a __ComObject, this because the __ComObject can be almost everything and can be casted to several interfaces, of course C# don't know what to return so it returns this special kind of "dynamic" object. These objects are called RCW that stands for Runtime Callable Wrapper.

一个很好的读可以的 http://www.codeproject.com/KB/cs/cominteropnet.aspx

这篇关于C#系统.__ ComObject访问的Excel文件时返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆