本文介绍了请问C#.NET支持的IDispatch后期绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我的问题是:请问C#nativly支持后期绑定的IDispatch


pretend 的我试图自动化办公,同时与客户已安装任何版本兼容。

Pretend i'm trying to automate Office, while being compatible with whatever version the customer has installed.

在.NET世界,如果你使用Office 2000安装,每个开发人员开发的,每一个客户,从现在到年底的时候,需要有Office 2000中。

In the .NET world if you developed with Office 2000 installed, every developer, and every customer, from now until the end of time, is required to have Office 2000.

在.NET之前的世界中,我们使用的 COM 交谈Office应用程序。

In the world before .NET, we used COM to talk to Office applications.

例如:

1)使用版本独立程序id

1) Use the version independant ProgID

"Excel.Application"

解析为:

clsid = {00024500-0000-0000-C000-000000000046}

,然后使用COM,我们要求这些类的一个实例化到一个对象:

and then using COM, we ask for one of these classes to be instantiated into an object:

IUnknown unk;
CoCreateInstance(
    clsid, 
    null,
    CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,
    IUnknown, 
    out unk);

而现在我们去比赛 - 能够使用Excel从我的应用程序中。当然,如果的真正的您要使用的对象,你要调用在调用方法的一些方式。

And now we're off to the races - able to use Excel from inside my application. Of course, if really you want to use the object, you have to call have some way of calling methods.

我们的可能的阿霍德不同的接口的声明,翻译成我们的语言。这种技术是好事,因为我们得到

We could get ahold of the various interface declarations, translated into our language. This technique is good because we get


  • 早期绑定

  • code-洞察

  • 编译型语法检查

和一些例如code可能是:

and some example code might be:

Application xl = (IExcelApplication)unk;
ExcelWorkbook workbook = xl.Workbooks.Add(template, lcid);
Worksheet worksheet = workbook.ActiveSheet;



但是,使用接口的一个缺点:我们必须得到阿霍德的各种接口声明,transated到我们的语言。而我们使用基于方法的调用卡住了,不必指定所有参数,例如:

But there is a downside of using interfaces: we have to get ahold of the various interface declarations, transated into our language. And we're stuck using method-based invocations, having to specify all parameters, e.g.:

ExcelWorkbook workbook = xl.Workbooks.Add(template, lcid);
xl.Worksheets.Add(before, after, count, type, lcid);

这证明,在现实世界中,有这样的缺点,我们会愿意放弃:

This has proved, in the real world, to have such downsides that we would willingly give up:


  • 早期绑定

  • code-洞察

  • 编译时语法检查

,而是使用的IDispatch 后期绑定:

Variant xl = (IDispatch)unk;
Variant newWorksheet = xl.Worksheets.Add();

由于Excel的自动化是专为VB脚本,很多的参数可以ommitted,即使是没有他们,没有超载。

Because Excel automation was designed for VB Script, a lot of parameters can be ommitted, even when there is no overload without them.

注意:不要混淆我的Excel例子有原因的,为什么我要使用IDispatch接口。并不是每一个COM对象是Excel。某些COM对象没有比通过IDispatch等支持。

Note: Don't confuse my example of Excel with a reason of why i want to use IDispatch. Not every COM object is Excel. Some COM objects have no support other than through IDispatch.

推荐答案

可以,relativly,使用后期绑定的IDispatch在C#约束力。

You can, relativly, use late-binding IDispatch binding in C#.

下面是使用Excel的一些示例。这样,您就不需要微软的bloaty PIA添加不必要的依赖关系:

Here's some sample for using Excel. This way you don't need to add a needless dependancy on Microsoft's bloaty PIA:

//Create XL
Object xl = Activator.CreateInstance(Type.GetTypeFromProgID("Excel.Application"));

//Get the workbooks collection.
//   books = xl.Workbooks;
Object books = xl.GetType().InvokeMember( "Workbooks", 
      BindingFlags.GetProperty, null, xl, null);

//Add a new workbook.
//   book = books.Add();
Objet book = books.GetType().InvokeMember( "Add", 
      BindingFlags.InvokeMethod, null, books, null );

//Get the worksheets collection.
//   sheets = book.Worksheets;
Object sheets = book.GetType().InvokeMember( "Worksheets",
      BindingFlags.GetProperty, null, book, null );

Object[] parameters;

//Get the first worksheet.
//   sheet = sheets.Item[1]
parameters = new Object[1];
parameters[0] = 1;
Object sheet = sheets.GetType().InvokeMember( "Item", 
      BindingFlags.GetProperty, null, sheets, parameters );

//Get a range object that contains cell A1.
//   range = sheet.Range["A1];
parameters = new Object[2];
parameters[0] = "A1";
parameters[1] = Missing.Value;
Object range = sheet.GetType().InvokeMember( "Range",
      BindingFlags.GetProperty, null, sheet, parameters );

//Write "Hello, World!" in cell A1.
//   range.Value = "Hello, World!";
parameters = new Object[1];
parameters[0] = "Hello, World!";
objRange_Late.GetType().InvokeMember( "Value", BindingFlags.SetProperty, 
      null, range, parameters );

//Return control of Excel to the user.
//   xl.Visible = true;
//   xl.UserControl = true;
parameters = new Object[1];
parameters[0] = true;
xl.GetType().InvokeMember( "Visible", BindingFlags.SetProperty,
      null, xl, Parameters );
xl.GetType().InvokeMember( "UserControl", BindingFlags.SetProperty,
      null, xl, Parameters );

这篇关于请问C#.NET支持的IDispatch后期绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 01:15