Using Reflection with COM Interop(将反射与 COM 互操作一起使用)
问题描述
在互操作调用之后,我得到了一个 COM 对象.我知道这个对象将是三个可能的 COM 类(Class1、Class2、Class3)之一,但不知道运行时是哪一个.
After an interop call, I get back a COM object. I know this object will be one of three possible COM classes (Class1, Class2, Class3), but do not know which one in runtime.
对该对象的反射 (interopObject.GetType()) 返回 System.__ComObject 的基本 RCW 包装器.
The reflection upon that object (interopObject.GetType()) returns the base RCW wrapper of System.__ComObject.
我需要在对象上设置一些属性 - Text1、Text2、... Text30(实际名称,顺便说一句:)),它们存在于所有三个类中.
What I need is to set some properties on the object - Text1, Text2, ... Text30 (actual names, btw :)), which exist in all three classes.
所以,问题是,我能否以某种方式获取对象的运行时类型(这将解决我的问题,但可能是不可能的,因为 .net 运行时可能没有该信息),或者我可以设置一个属性盲目的COM对象
So, the question is, can I somehow get the runtime type of the object (this would solve my problem, but might be impossible, as the .net runtime might not have that info), or can I set a property of a COM object blindly
这是我当前的代码,但失败了:
this is my current code, which fails:
for ( int i = 1; i <= 30; i++ )
{
ProprertyInfo pi =interopObject.GetType().GetProperty("Text" +i.ToString())
// this returns null for pi
pi.GetSetMethod().Invoke(interopObject, new object[] { someValue });
}
<小时>
感谢 Marc,这三个都进入了我的永久噱头收藏:
Thanks to Marc, these three go in my permanent gimmicks collection:
private static object LateGetValue(object obj, string propertyName)
{
return RuntimeHelpers.GetObjectValue(NewLateBinding.LateGet(obj, null,
propertyName, new object[0], null, null, null));
}
private static void LateSetValue(object obj, string propertyName, object value)
{
NewLateBinding.LateSet(obj, null, propertyName, new []{value}, null, null);
}
private static void LateCallMethod(object obj, string methodName)
{
NewLateBinding.LateCall(obj, null, methodName, new object[0], null,
null, null, true);
}
推荐答案
在 C# 4.0 中,dynamic
非常适合这种类型的鸭子类型.
In C# 4.0, dynamic
would be ideal for this type of duck-typing.
在那之前,我想知道 VB.Net 是否会更好,使用 Option Strict Off
以允许对 object
进行后期绑定.
Until then, I wonder if VB.Net would be better, with Option Strict Off
to allow late binding against object
.
最坏的情况:用VB.Net写,然后用reflector为你写C#;-p
Worst case: write it in VB.Net, then use reflector to write the C# for you ;-p
这是一个示例,需要引用 Microsoft.VisualBasic.dll,但在 C# 中很好:
Here's an example, that requires a reference to Microsoft.VisualBasic.dll, but is fine in C#:
public static object GetValue(object obj, string propertyName)
{
return RuntimeHelpers.GetObjectValue(NewLateBinding.LateGet(obj, null,
propertyName, new object[0], null, null, null));
}
这篇关于将反射与 COM 互操作一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将反射与 COM 互操作一起使用
基础教程推荐
- c# Math.Sqrt 实现 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 XML 转换为通用列表 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01