COM interface wrappers in PowerShell?

不羁的心 提交于 2019-12-04 10:12:54
x0n

PowerShell special-cases COM objects with its own late-binding "COM Adapter" in order to expose members to callers (and the Get-Member cmdlet). Unfortunately this sometimes fails if it cannot find the associated type library, which usually happens when the instance is actually a remote COM object surfaced through a transparentproxy type.

Another side-effect of this COM adaptation is that you are indirectly prevented from using these kind of casts to gain access to members. PowerShell generally exposes the interop assembly's (dynamically created or PIA) CoClass class, which includes members of all interfaces. In fact, this limitation on interfaces is not just with COM objects: the ".NET Adapter" in PowerShell doesn't deal with plain old .NET interfaces either. To be honest, this is the preferred behaviour for 99% of cases. PowerShell is a dynamic language and will always expose the true type of a reference at runtime. Any attempts to cast to an interface will be ignored.

This leads to more problems when you get to explicitly implemented interfaces in C#. PowerShell can't see them at all! I did blog about a technique to proxy explicit interface members using a v2.0 module. You could try it against a COM interface, but I'm not sure it would work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!