invokeMember object does not match target type错误解决办法

4 篇文章 0 订阅

有个项目中需要将C#写的dll中产生一个事件event,然后C++程序调用,并注册回调事件。触发事件的时候,报了个错,而在开发者机器上又没有出现,开始以为是环境问题,可是又找不到具体是哪个环境没装,最后将vs在目标机器上装了,还是不行,,,结果是将C#程序在那机器上编译一逼,才没有出现。。。所以这个问题还是没有解决(不可能每个机子都去编译一遍吧!!!)

网上找了很久,终于在bing上搜到一个帖子,搞得,不容易啊,不得不感谢外国人。。。,有些问题还是得靠英文搜索啊。

我的搜索的关机字就是“invokeMember object does not match target type”,以下是帖子的核心内容。

This is a threading issue, I think.

The call to Completed is of course a call to a COM object. The call is probably marshalled to another thread in the COM than the main thread, which will produce the error "Object does not match target type".

Instead of this:

if ( this.Completed != null )
    this.Completed( true );

...try this:

if ( this.Completed != null )
    Invoke(Completed, new object[] { true });

Take a look at the documentation for the InvokeRequired property.

 An alternative approach would be to declare the Completed method as static in VB.

帖子链接:

https://social.msdn.microsoft.com/Forums/vstudio/en-US/e3a3930f-fa50-4959-8d51-2e345a8214bf/object-does-not-match-target-type-when-raising-an-event-from-c-to-vb6

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用C#代码实现读取Excel表格并判断单元格是否为空的方法,不使用using指令和程序集引用: ```csharp using System; using System.Reflection; using System.Runtime.InteropServices; class Program { static void Main(string[] args) { // 打开Excel应用程序 Type excelType = Type.GetTypeFromProgID("Excel.Application"); object excelObj = Activator.CreateInstance(excelType); // 打开Excel文件 object workbookObj = excelType.InvokeMember("Workbooks", BindingFlags.GetProperty, null, excelObj, null); object[] args1 = { "D:\\test.xlsx" }; object workbook = workbookObj.GetType().InvokeMember("Open", BindingFlags.InvokeMethod, null, workbookObj, args1); // 获取第一个工作表 object sheetsObj = workbook.GetType().InvokeMember("Sheets", BindingFlags.GetProperty, null, workbook, null); object sheet1 = sheetsObj.GetType().InvokeMember("Item", BindingFlags.GetProperty, null, sheetsObj, new object[] { 1 }); // 获取工作表的行数和列数 int rowCount = (int)sheet1.GetType().InvokeMember("UsedRange", BindingFlags.GetProperty, null, sheet1, null).GetType().InvokeMember("Rows", BindingFlags.GetProperty, null, null, null); int columnCount = (int)sheet1.GetType().InvokeMember("UsedRange", BindingFlags.GetProperty, null, sheet1, null).GetType().InvokeMember("Columns", BindingFlags.GetProperty, null, null, null); // 遍历每个单元格 for (int i = 1; i <= rowCount; i++) { for (int j = 1; j <= columnCount; j++) { object cell = sheet1.GetType().InvokeMember("Cells", BindingFlags.GetProperty, null, sheet1, new object[] { i, j }); object value = cell.GetType().InvokeMember("Value", BindingFlags.GetProperty, null, cell, null); if (value == null) { cell.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, cell, new object[] { "单元格为空" }); } } } // 关闭Excel文件并退出Excel应用程序 workbook.GetType().InvokeMember("Close", BindingFlags.InvokeMethod, null, workbook, new object[] { false }); excelType.InvokeMember("Quit", BindingFlags.InvokeMethod, null, excelObj, null); Marshal.ReleaseComObject(sheet1); Marshal.ReleaseComObject(sheetsObj); Marshal.ReleaseComObject(workbook); Marshal.ReleaseComObject(workbookObj); Marshal.ReleaseComObject(excelObj); GC.Collect(); } } ``` 在这段代码中,我们使用了反射的方式来调用Excel应用程序的各种属性和方法,而没有使用using指令和程序集引用。具体实现步骤如下: 1. 使用Type.GetTypeFromProgID方法获取Excel应用程序的类型对象; 2. 使用Activator.CreateInstance方法创建Excel应用程序的实例; 3. 使用Workbooks属性获取Excel文件集合对象,使用InvokeMember方法调用Open方法打开Excel文件; 4. 使用Sheets属性获取工作表集合对象,使用Item属性获取第一个工作表; 5. 使用UsedRange属性获取工作表的使用范围,使用Rows属性和Columns属性获取行数和列数; 6. 使用Cells属性获取单元格对象,使用Value属性获取单元格的值,判断单元格是否为空,如果为空则使用Value属性设置单元格的值为"单元格为空"; 7. 使用Close方法关闭Excel文件,使用Quit方法退出Excel应用程序; 8. 使用Marshal.ReleaseComObject方法释放COM对象,使用GC.Collect方法回收内存。 以上就是使用C#代码实现读取Excel表格并判断单元格是否为空的方法,不使用using指令和程序集引用的完整代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值