通过COM对象的反射获取属性名称 [英] Get property names via reflection of a COM object

查看:463
本文介绍了通过COM对象的反射获取属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么可以读出通过在C#中的COM对象反射所有属性名?
我知道如何获得的财产,如果我知道这个名字。

How can I read out all property names via reflection of an COM Object in C#? I know how to get the property if I know the name.

comObject.GetType().InvokeMember("PropertyName", System.Reflection.BindingFlags.GetProperty, null, comObject, null);

但什么是当我想转储所有属性?

but what is when I want to dump all properties?

PropertyInfo[] properties = t.GetProperties();

这种方式不与COM的对象。如果我做一个GetMembers()我得到这些成员:

This way didn't work with Com-Objects. If I do a GetMembers() I get these Members:

Name: GetLifetimeService
Name: InitializeLifetimeService
Name: CreateObjRef
Name: ToString
Name: Equals
Name: GetHashCode
Name: GetType

至于克里斯

推荐答案

您得到了__ComObject类的成员,为RCW基础.NET类。

You got the members of the __ComObject class, the underlying .NET class for an RCW.

COM具有反射一丁点的支持,只要COM组件类实现IDispatch,自动化接口。不寻常,它是一个脚本语言使用拨打电话的接口。 IDispatch.GetIDsOfNames()总是实现的,但你必须知道该成员前面的名称。 IDispatch.GetTypeInfo()可以访问类型库的组件类,但并不总是实现。元数据的类型库的信息翻译是一个不完美的艺术,最核心的原因,为什么.NET要你做这个前期与Tlbimp.exe将。如果你想追求一个运行方式,那么你可能从源头上受益TLBIMP的托管版本,的可在这里

COM has a wee bit support for reflection, as long as the COM coclass implements IDispatch, the Automation interface. Not unusual, it is the interface that scripting languages use to make calls. IDispatch.GetIDsOfNames() is always implemented, but you have to know the name of the member up front. IDispatch.GetTypeInfo() gives access to the type library for the coclass, but isn't always implemented. Translation of type library info to metadata is an imperfect art, the core reason why .NET wants you to do this upfront with Tlbimp.exe. If you want to pursue a runtime approach then you'll probably benefit from the source for the managed version of Tlbimp, available here.

这样做了前面在编译的时候总是最好的,你通过向类型库的引用(通常嵌入在可执行文件)或通过运行Tlbimp.exe将自己这样做。你现在明白了有所有底层的COM组件类和接口的成员.NET互操作程序包装类。在这一点上,你可能不需要再反思:)

Doing this up front at build time is always best, you do so by adding a reference to the type library (usually embedded in the executable file) or by running Tlbimp.exe yourself. Now you got a .NET interop wrapper class that has all of the members of the underlying COM coclass and interfaces. At which point you probably don't need reflection anymore :)

这篇关于通过COM对象的反射获取属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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