使用反射与COM互操作 [英] Using Reflection with COM Interop

查看:509
本文介绍了使用反射与COM互操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个互操作调用后,我得到一个COM对象。
我知道这个对象将是三种可能的COM类(1级,等级2,3类)之一,但不知道哪一个在运行。



反射。在该对象(interopObject.GetType())返回系统.__ ComObject的基础RCW包装



我需要做的是在对象上设置一些属性 - 文本1,文本2 ... Text30(实际名称,顺便说一句:)),它存在于所有三个等级。



所以,问题是,我可以以某种方式得到的运行时类型对象(这将解决我的问题,但可能是不可能的,因为.NET运行时可能不会有这些信息),或我设置一个COM对象盲目



的属性

这是我当前的代码,这将失败:

 的for(int i = 1; I< = 30;我++) 
{
ProprertyInfo PI = interopObject.GetType()的getProperty(文本+ i.ToString())
//这个返回null PI
pi.GetSetMethod() .Invoke(interopObject,新的对象[] {someValue中});
}






由于马克,这三个走在我的永久噱头系列:

 私有静态对象LateGetValue(obj对象,字符串propertyName的)
{
返回RuntimeHelpers.GetObjectValue(NewLateBinding.LateGet(OBJ,空,
propertyName的,新的对象[0]​​,NULL,NULL,NULL));
}

私有静态无效LateSetValue(obj对象,字符串propertyName的,对象的值)
{
NewLateBinding.LateSet(OBJ,空,propertyName的,新的[] {值},NULL,NULL);
}

私有静态无效LateCallMethod(obj对象,字符串methodName中)
{
NewLateBinding.LateCall(OBJ,空,方法名,新的对象[0]​​,空,
NULL,NULL,TRUE);
}


解决方案

在C#4.0,动态将是理想的这种类型的鸭打字。



在此之前,我不知道如果VB.Net会更好与选项严格关闭允许对对象后期绑定。



最坏的情况:它在VB.Net编写,然后使用反射镜为你;-p编写C#



下面是一个例子,这需要微软的引用.VisualBasic.dll,但在C#罚款:

 公共静态对象的GetValue(obj对象,字符串propertyName的)
{
返回RuntimeHelpers.GetObjectValue(NewLateBinding.LateGet(OBJ,空,
propertyName的,新的对象[0]​​,NULL,NULL,NULL));
}


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.

The reflection upon that object (interopObject.GetType()) returns the base RCW wrapper of System.__ComObject.

What I need is to set some properties on the object - Text1, Text2, ... Text30 (actual names, btw :)), which exist in all three classes.

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 });
}


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);
}

解决方案

In C# 4.0, dynamic would be ideal for this type of duck-typing.

Until then, I wonder if VB.Net would be better, with Option Strict Off to allow late binding against object.

Worst case: write it in VB.Net, then use reflector to write the C# for you ;-p

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互操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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