如何识别"默认会员"在COM库中?

时间:2016-03-16 16:14:01

标签: c# com

我使用ITypeInfo API来"翻译" COM类型库进入"声明"我的应用程序使用的对象。

"声明的特征之一"是"属性" - 我能告诉他们什么时候发布财产"声明"应该有一个{name}.VB_UserMemId = -4属性(即当它是_NewEnum不可浏览的属性时;我也可以告诉属性何时隐藏。

我未能说出的是,当某个成员是该类型的默认成员时。根据{{​​1}}(MSDN)的说明:

  

最能代表对象的函数。类型信息中只有一个函数可以具有此属性。

......这听起来非常像我正在寻找的东西。然而,在所有成员中,我遍历了少数几个类型库(包括VBA标准库和整个Excel对象模型),正好都有该标志。

我希望此代码可以FUNCFLAG_FDEFAULTBINDAddDefaultMemberAttribute之类的内容调用Collection.Item,这是类型的默认成员 - 但没有骰子。我没有检查正确的标志/值吗?

var attributes = new Attributes();
if (((FUNCFLAGS)memberDescriptor.wFuncFlags) != 0)
{
    if (memberName == "_NewEnum" && ((FUNCFLAGS)memberDescriptor.wFuncFlags).HasFlag(FUNCFLAGS.FUNCFLAG_FNONBROWSABLE))
    {
        attributes.AddEnumeratorMemberAttribute(memberName);
    }
    else if (((FUNCFLAGS)memberDescriptor.wFuncFlags).HasFlag(FUNCFLAGS.FUNCFLAG_FHIDDEN))
    {
        attributes.AddHiddenMemberAttribute(memberName);
    }
    else if (((FUNCFLAGS)memberDescriptor.wFuncFlags).HasFlag(FUNCFLAGS.FUNCFLAG_FDEFAULTBIND))
    {
        // bug: none of the 30K+ reflected members have this flag on??
        attributes.AddDefaultMemberAttribute(memberName);
    }
}

1 个答案:

答案 0 :(得分:0)

取消对wFuncFlags != 0的检查,您正在使用该支票跳过大量成员,其中很多是您真正感兴趣的。

现在检查memid == 0thanks @HansPassant

else if (memberDescriptor.memid == 0)
{
    attributes.AddDefaultMemberAttribute(memberName);
    Debug.WriteLine("Default member found: {0}.{1} ({2} / {3})", moduleDeclaration.IdentifierName, memberName, memberDeclarationType, (VarEnum)memberDescriptor.elemdescFunc.tdesc.vt);
}

这个小Debug.WriteLine指令产生这个输出(截断,以适应后期字符限制):

Default member found: _ErrObject.Number (PropertyGet / VT_I4)
Default member found: _ErrObject.Number (PropertyLet / VT_VOID)
Default member found: _Collection.Item (Function / VT_VARIANT)
Default member found: Adjustments.Item (PropertyGet / VT_R4)
Default member found: Adjustments.Item (PropertyLet / VT_VOID)
Default member found: ColorFormat.RGB (PropertyGet / VT_USERDEFINED)
Default member found: ColorFormat.RGB (PropertyLet / VT_VOID)
Default member found: ShapeNodes.Item (Function / VT_PTR)
Default member found: DiagramNodes.Item (Function / VT_PTR)
Default member found: DiagramNodeChildren.Item (Function / VT_PTR)
Default member found: IWindows._Default (PropertyGet / VT_HRESULT)
Default member found: _Application._Default (PropertyGet / VT_BSTR)
Default member found: IRange._Default (PropertyGet / VT_HRESULT)
Default member found: IRange._Default (PropertyLet / VT_HRESULT)
Default member found: Sheets._Default (PropertyGet / VT_DISPATCH)
Default member found: IHPageBreaks._Default (PropertyGet / VT_HRESULT)
Default member found: IVPageBreaks._Default (PropertyGet / VT_HRESULT)
Default member found: IRecentFiles._Default (PropertyGet / VT_HRESULT)
Default member found: IStyle._Default (PropertyGet / VT_HRESULT)
Default member found: IStyles._Default (PropertyGet / VT_HRESULT)
Default member found: IBorders._Default (PropertyGet / VT_HRESULT)
Default member found: IAddIns._Default (PropertyGet / VT_HRESULT)
Default member found: IToolbars._Default (PropertyGet / VT_HRESULT)
Default member found: IToolbarButtons._Default (PropertyGet / VT_HRESULT)
Default member found: IAreas._Default (PropertyGet / VT_HRESULT)
Default member found: Workbooks._Default (PropertyGet / VT_PTR)
Default member found: IMenuBars._Default (PropertyGet / VT_HRESULT)
Default member found: IMenus._Default (PropertyGet / VT_HRESULT)
Default member found: IMenuItems._Default (PropertyGet / VT_HRESULT)
Default member found: ICharts._Default (PropertyGet / VT_HRESULT)
Default member found: IDrawingObjects._Default (PropertyGet / VT_HRESULT)
Default member found: IDrawingObjects._Default (PropertyLet / VT_HRESULT)
Default member found: IPivotCaches._Default (PropertyGet / VT_HRESULT)
Default member found: IPivotFormula._Default (PropertyGet / VT_HRESULT)
Default member found: IPivotFormula._Default (PropertyLet / VT_HRESULT)
Default member found: IPivotFormulas._Default (PropertyGet / VT_HRESULT)
Default member found: IPivotTable._Default (PropertyGet / VT_HRESULT)
Default member found: IPivotTable._Default (PropertyLet / VT_HRESULT)
Default member found: IPivotField._Default (PropertyGet / VT_HRESULT)
Default member found: IPivotField._Default (PropertyLet / VT_HRESULT)
Default member found: ICalculatedFields._Default (PropertyGet / VT_HRESULT)
Default member found: IPivotItem._Default (PropertyGet / VT_HRESULT)
Default member found: IPivotItem._Default (PropertyLet / VT_HRESULT)
Default member found: ICalculatedItems._Default (PropertyGet / VT_HRESULT)
Default member found: IDialogs._Default (PropertyGet / VT_HRESULT)
Default member found: ICheckBox._Default (PropertyGet / VT_HRESULT)
Default member found: ICheckBox._Default (PropertyLet / VT_HRESULT)
Default member found: ICheckBoxes._Default (PropertyGet / VT_HRESULT)
Default member found: ICheckBoxes._Default (PropertyLet / VT_HRESULT)
Default member found: IOptionButton._Default (PropertyGet / VT_HRESULT)
Default member found: IOptionButton._Default (PropertyLet / VT_HRESULT)
Default member found: IOptionButtons._Default (PropertyGet / VT_HRESULT)
Default member found: IOptionButtons._Default (PropertyLet / VT_HRESULT)
Default member found: IScrollBar._Default (PropertyGet / VT_HRESULT)
Default member found: IScrollBar._Default (PropertyLet / VT_HRESULT)
Default member found: IScrollBars._Default (PropertyGet / VT_HRESULT)
Default member found: IScrollBars._Default (PropertyLet / VT_HRESULT)
Default member found: IListBox._Default (PropertyGet / VT_HRESULT)
Default member found: IListBox._Default (PropertyLet / VT_HRESULT)
Default member found: IListBoxes._Default (PropertyGet / VT_HRESULT)
Default member found: IListBoxes._Default (PropertyLet / VT_HRESULT)
Default member found: IDropDown._Default (PropertyGet / VT_HRESULT)
Default member found: IDropDown._Default (PropertyLet / VT_HRESULT)
Default member found: IDropDowns._Default (PropertyGet / VT_HRESULT)
Default member found: IDropDowns._Default (PropertyLet / VT_HRESULT)
Default member found: ISpinner._Default (PropertyGet / VT_HRESULT)
Default member found: ISpinner._Default (PropertyLet / VT_HRESULT)
Default member found: ISpinners._Default (PropertyGet / VT_HRESULT)
Default member found: ISpinners._Default (PropertyLet / VT_HRESULT)
Default member found: IPanes._Default (PropertyGet / VT_HRESULT)
Default member found: IGroupObject._Default (PropertyGet / VT_HRESULT)
Default member found: IGroupObject._Default (PropertyLet / VT_HRESULT)
Default member found: IGroupObjects._Default (PropertyGet / VT_HRESULT)
Default member found: IGroupObjects._Default (PropertyLet / VT_HRESULT)
Default member found: IModules._Default (PropertyGet / VT_HRESULT)
Default member found: IDialogSheets._Default (PropertyGet / VT_HRESULT)
Default member found: IWorksheets._Default (PropertyGet / VT_HRESULT)
Default member found: INames._Default (Function / VT_HRESULT)
Default member found: IName._Default (PropertyGet / VT_HRESULT)
Default member found: IChartObjects._Default (Function / VT_HRESULT)
Default member found: ICustomViews._Default (PropertyGet / VT_HRESULT)
Default member found: IFormatConditions._Default (PropertyGet / VT_HRESULT)
Default member found: IComments._Default (PropertyGet / VT_HRESULT)
Default member found: IQueryTables._Default (PropertyGet / VT_HRESULT)
Default member found: IParameters._Default (PropertyGet / VT_HRESULT)
Default member found: IODBCErrors._Default (PropertyGet / VT_HRESULT)
Default member found: IHyperlinks._Default (PropertyGet / VT_HRESULT)
Default member found: IFilters._Default (PropertyGet / VT_HRESULT)
Default member found: IChartColorFormat._Default (PropertyGet / VT_HRESULT)
Default member found: IAxes._Default (Function / VT_HRESULT)
Default member found: IPoints._Default (Function / VT_HRESULT)
Default member found: ISeriesCollection._Default (Function / VT_HRESULT)
Default member found: IDataLabels._Default (Function / VT_HRESULT)
Default member found: ILegendEntries._Default (Function / VT_HRESULT)
Default member found: ITrendlines._Default (Function / VT_HRESULT)
Default member found: IShapes._Default (Function / VT_HRESULT)
Default member found: IShapeRange._Default (Function / VT_HRESULT)
Default member found: IGroupShapes._Default (Function / VT_HRESULT)
Default member found: IControlFormat._Default (PropertyGet / VT_HRESULT)
Default member found: IControlFormat._Default (PropertyLet / VT_HRESULT)
Default member found: IPublishObjects._Default (PropertyGet / VT_HRESULT)
Default member found: IOLEDBErrors._Default (PropertyGet / VT_HRESULT)
Default member found: IPhonetics._Default (PropertyGet / VT_HRESULT)
Default member found: CubeFields._Default (PropertyGet / VT_PTR)
Default member found: IUsedObjects._Default (PropertyGet / VT_HRESULT)
Default member found: ICustomProperties._Default (PropertyGet / VT_HRESULT)
Default member found: ICustomProperty._Default (PropertyGet / VT_HRESULT)
Default member found: ICalculatedMembers._Default (PropertyGet / VT_HRESULT)
Default member found: ICalculatedMember._Default (PropertyGet / VT_HRESULT)
Default member found: IWatches._Default (PropertyGet / VT_HRESULT)
Default member found: IErrors._Default (PropertyGet / VT_HRESULT)
Default member found: ISmartTagAction._Default (PropertyGet / VT_HRESULT)
Default member found: ISmartTagActions._Default (PropertyGet / VT_HRESULT)
Default member found: ISmartTag._Default (PropertyGet / VT_HRESULT)
Default member found: ISmartTags._Default (PropertyGet / VT_HRESULT)
Default member found: ISmartTagRecognizer._Default (PropertyGet / VT_HRESULT)
Default member found: ISmartTagRecognizers._Default (PropertyGet / VT_HRESULT)
Default member found: IPivotItemList._Default (PropertyGet / VT_HRESULT)
Default member found: IAllowEditRanges._Default (PropertyGet / VT_HRESULT)
Default member found: IUserAccessList._Default (PropertyGet / VT_HRESULT)
Default member found: IListObjects._Default (PropertyGet / VT_HRESULT)
Default member found: IListObject._Default (PropertyGet / VT_HRESULT)
Default member found: IListColumns._Default (PropertyGet / VT_HRESULT)
Default member found: IListColumn._Default (PropertyGet / VT_HRESULT)
Default member found: IListRows._Default (PropertyGet / VT_HRESULT)
Default member found: IXmlNamespace._Default (PropertyGet / VT_HRESULT)
Default member found: IXmlNamespaces._Default (PropertyGet / VT_HRESULT)
Default member found: IXmlDataBinding._Default (PropertyGet / VT_HRESULT)
Default member found: IXmlSchemas._Default (PropertyGet / VT_HRESULT)
Default member found: IXmlMap._Default (PropertyGet / VT_HRESULT)
Default member found: IXmlMaps._Default (PropertyGet / VT_HRESULT)
Default member found: IListDataFormat._Default (PropertyGet / VT_HRESULT)
Default member found: IXPath._Default (PropertyGet / VT_HRESULT)
Default member found: IPivotLineCells._Default (PropertyGet / VT_HRESULT)
Default member found: IPivotLines._Default (PropertyGet / VT_HRESULT)
Default member found: IPivotFilters._Default (PropertyGet / VT_HRESULT)
Default member found: IWorkbookConnection._Default (PropertyGet / VT_HRESULT)
Default member found: IWorkbookConnection._Default (PropertyLet / VT_HRESULT)
Default member found: IConnections._Default (PropertyGet / VT_HRESULT)
Default member found: ISheetViews._Default (PropertyGet / VT_HRESULT)
Default member found: IActions._Default (PropertyGet / VT_HRESULT)
Default member found: IColorScaleCriteria._Default (PropertyGet / VT_HRESULT)
Default member found: IIconCriteria._Default (PropertyGet / VT_HRESULT)
Default member found: IIconSet._Default (PropertyGet / VT_HRESULT)
Default member found: IIconSets._Default (PropertyGet / VT_HRESULT)
Default member found: IRanges._Default (PropertyGet / VT_HRESULT)
Default member found: IPages._Default (PropertyGet / VT_HRESULT)
Default member found: IServerViewableItems._Default (PropertyGet / VT_HRESULT)
Default member found: ITableStyleElements._Default (PropertyGet / VT_HRESULT)
Default member found: ITableStyle._Default (PropertyGet / VT_HRESULT)
Default member found: ITableStyles._Default (PropertyGet / VT_HRESULT)
Default member found: ISortFields._Default (PropertyGet / VT_HRESULT)
Default member found: IColorStops._Default (PropertyGet / VT_HRESULT)
Default member found: IFileExportConverters._Default (PropertyGet / VT_HRESULT)
Default member found: IAddIns2._Default (PropertyGet / VT_HRESULT)
Default member found: ISparklineGroups._Default (PropertyGet / VT_HRESULT)
Default member found: IPivotTableChangeList._Default (PropertyGet / VT_HRESULT)
Default member found: ISlicerCaches._Default (PropertyGet / VT_HRESULT)
Default member found: ISlicerCacheLevels._Default (PropertyGet / VT_HRESULT)
Default member found: ISlicers._Default (PropertyGet / VT_HRESULT)
Default member found: ISlicerItems._Default (PropertyGet / VT_HRESULT)
Default member found: ISlicerPivotTables._Default (PropertyGet / VT_HRESULT)
Default member found: IProtectedViewWindows._Default (PropertyGet / VT_HRESULT)
Default member found: IProtectedViewWindow._Default (PropertyGet / VT_HRESULT)
Default member found: Windows._Default (PropertyGet / VT_PTR)
Default member found: Range._Default (PropertyGet / VT_VARIANT)
Default member found: Range._Default (PropertyLet / VT_VOID)
Default member found: HPageBreaks._Default (PropertyGet / VT_PTR)
Default member found: VPageBreaks._Default (PropertyGet / VT_PTR)
Default member found: RecentFiles._Default (PropertyGet / VT_PTR)
Default member found: Style._Default (PropertyGet / VT_BSTR)
Default member found: Styles._Default (PropertyGet / VT_PTR)
Default member found: Borders._Default (PropertyGet / VT_PTR)
Default member found: AddIns._Default (PropertyGet / VT_PTR)
Default member found: Toolbars._Default (PropertyGet / VT_PTR)
Default member found: ToolbarButtons._Default (PropertyGet / VT_PTR)
Default member found: Areas._Default (PropertyGet / VT_PTR)
Default member found: MenuBars._Default (PropertyGet / VT_PTR)
Default member found: Menus._Default (PropertyGet / VT_PTR)
Default member found: MenuItems._Default (PropertyGet / VT_DISPATCH)
Default member found: Charts._Default (PropertyGet / VT_DISPATCH)
Default member found: DrawingObjects._Default (PropertyGet / VT_I4)
Default member found: DrawingObjects._Default (PropertyLet / VT_VOID)
Default member found: PivotCaches._Default (PropertyGet / VT_PTR)
Default member found: PivotFormula._Default (PropertyGet / VT_BSTR)
Default member found: PivotFormula._Default (PropertyLet / VT_VOID)
Default member found: PivotFormulas._Default (PropertyGet / VT_PTR)
Default member found: PivotTable._Default (PropertyGet / VT_BSTR)
Default member found: PivotTable._Default (PropertyLet / VT_VOID)
Default member found: PivotField._Default (PropertyGet / VT_BSTR)
Default member found: PivotField._Default (PropertyLet / VT_VOID)
Default member found: CalculatedFields._Default (PropertyGet / VT_PTR)
Default member found: PivotItem._Default (PropertyGet / VT_BSTR)
Default member found: PivotItem._Default (PropertyLet / VT_VOID)
Default member found: CalculatedItems._Default (PropertyGet / VT_PTR)
Default member found: Dialogs._Default (PropertyGet / VT_PTR)
Default member found: CheckBox._Default (PropertyGet / VT_I4)
Default member found: CheckBox._Default (PropertyLet / VT_VOID)
Default member found: CheckBoxes._Default (PropertyGet / VT_I4)
Default member found: CheckBoxes._Default (PropertyLet / VT_VOID)
Default member found: OptionButton._Default (PropertyGet / VT_I4)
Default member found: OptionButton._Default (PropertyLet / VT_VOID)
Default member found: OptionButtons._Default (PropertyGet / VT_I4)
Default member found: OptionButtons._Default (PropertyLet / VT_VOID)
Default member found: ScrollBar._Default (PropertyGet / VT_I4)
Default member found: ScrollBar._Default (PropertyLet / VT_VOID)
Default member found: ScrollBars._Default (PropertyGet / VT_I4)
Default member found: ScrollBars._Default (PropertyLet / VT_VOID)
Default member found: ListBox._Default (PropertyGet / VT_I4)
Default member found: ListBox._Default (PropertyLet / VT_VOID)
Default member found: ListBoxes._Default (PropertyGet / VT_I4)
Default member found: ListBoxes._Default (PropertyLet / VT_VOID)
Default member found: DropDown._Default (PropertyGet / VT_I4)
Default member found: DropDown._Default (PropertyLet / VT_VOID)
Default member found: DropDowns._Default (PropertyGet / VT_I4)
Default member found: DropDowns._Default (PropertyLet / VT_VOID)
Default member found: Spinner._Default (PropertyGet / VT_I4)
Default member found: Spinner._Default (PropertyLet / VT_VOID)
Default member found: Spinners._Default (PropertyGet / VT_I4)
Default member found: Spinners._Default (PropertyLet / VT_VOID)
...

所以,你得到了你所追求的Collection.ItemErrObject.Number。 [另一个]现在的问题是,如何获取此_Default事物指向的成员的名称,几乎所有其他默认成员都在那里。

但这是一个开始。