简体   繁体   English

是否可以确定COM互操作类型中可选参数的默认值?

[英]Is it possible to determine default values of optional parameters in a COM interop type?

I am using C# 4.0 to instantiate an Excel.Application and open an Excel.Workbook . 我使用C#4.0实例化Excel.Application并打开Excel.Workbook Stripped down, my code looks like this: 剥离,我的代码看起来像这样:

Excel.Application xlApp;
Excel.Workbook xlWorkBook;

xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open("someFile");

The Open() method has a load of optional values that I do not need to provide. Open()方法有一些我不需要提供的可选值。 That is convenient, except that I do not know what the default values for those optional parameters are. 这很方便,除了我不知道这些可选参数的默认值是什么。 As such, I can not decide whether or not I have to provide a value. 因此,我无法决定是否必须提供价值。

In this case, I can find the desired information for some parameters in the MSDN documentation. 在这种情况下,我可以在MSDN文档中找到某些参数的所需信息。 Is there a generic way to determine the default values for optional parameters in a COM interop type? 是否有通用的方法来确定COM互操作类型中可选参数的默认值?

UPDATE: 更新:

Using oleview on MSO.dll , I checked out the corresponding IDL for the Workbook interface. MSO.dll上使用oleview ,我检查了Workbook接口的相应IDL。 For the SaveAs() method, it looks like this: 对于SaveAs()方法,它看起来像这样:

HRESULT SaveAs(
    [in, optional] VARIANT Filename,
    [in, optional] VARIANT FileFormat,
    [in, optional] VARIANT Password,
    blahblah...
    [in, optional, defaultvalue(1)] XlSaveAsAccessMode AccessMode,
    blahblah...
    [in, optional] VARIANT Local,
    [in, lcid] long lcid);

In Visual Studio, intellisense shows [object Filename = Type.Missing] for most parameters, which I guess makes sense because the IDL does not give any information about type ( VARIANT ) nor about any default value. 在Visual Studio中,intellisense为大多数参数显示[object Filename = Type.Missing] ,我认为这是有意义的,因为IDL不提供有关类型( VARIANT )的任何信息,也不提供任何默认值。 For the parameter AccessMode however, the IDL provides the required information and intellisense shows it. 但是,对于参数AccessMode ,IDL提供所需的信息,而intellisense则显示它。

So I guess the question now becomes: why is the IDL not more specific about the type (and default value) of all parameters, just like it is for AccessMode ? 所以我想现在的问题就是:为什么IDL没有更具体地说明所有参数的类型(和默认值),就像它对于AccessMode

why is the IDL not more specific about the type (and default value) 为什么IDL没有更具体的类型(和默认值)

It is specific about the type, it is VARIANT. 特定的类型,它是VARIANT。 It doesn't have to be specific about the default value, a variant is capable of specifying "not specified". 没有要具体有关默认值,变异体能够指定“未指定”的。 Actually specifying the default value in the type library is possible, you saw that from the reverse-engineered IDL. 实际上可以在类型库中指定默认值,您可以从反向工程IDL中看到它。

Lots of flexibility here. 这里有很多灵活性。 But if the variant value is "not specified" then the COM server will substitute whatever value it considers the default or give specific behavior to it not being specified. 但是如果变量值是“未指定”,那么COM服务器将替换它认为是默认值的任何值,或者给它指定未指定的特定行为。 The only way to find out what that might be is through the documentation. 找出可能是什么的唯一方法是通过文档。 Which indeed isn't always stellar. 这确实不总是很棒。

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

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