<i id='Kwmyo'><tr id='Kwmyo'><dt id='Kwmyo'><q id='Kwmyo'><span id='Kwmyo'><b id='Kwmyo'><form id='Kwmyo'><ins id='Kwmyo'></ins><ul id='Kwmyo'></ul><sub id='Kwmyo'></sub></form><legend id='Kwmyo'></legend><bdo id='Kwmyo'><pre id='Kwmyo'><center id='Kwmyo'></center></pre></bdo></b><th id='Kwmyo'></th></span></q></dt></tr></i><div id='Kwmyo'><tfoot id='Kwmyo'></tfoot><dl id='Kwmyo'><fieldset id='Kwmyo'></fieldset></dl></div>

<small id='Kwmyo'></small><noframes id='Kwmyo'>

<legend id='Kwmyo'><style id='Kwmyo'><dir id='Kwmyo'><q id='Kwmyo'></q></dir></style></legend>

      • <bdo id='Kwmyo'></bdo><ul id='Kwmyo'></ul>
      1. <tfoot id='Kwmyo'></tfoot>

        将反射与 COM 互操作一起使用

        时间:2023-07-26

            <tbody id='h6L2g'></tbody>
            <i id='h6L2g'><tr id='h6L2g'><dt id='h6L2g'><q id='h6L2g'><span id='h6L2g'><b id='h6L2g'><form id='h6L2g'><ins id='h6L2g'></ins><ul id='h6L2g'></ul><sub id='h6L2g'></sub></form><legend id='h6L2g'></legend><bdo id='h6L2g'><pre id='h6L2g'><center id='h6L2g'></center></pre></bdo></b><th id='h6L2g'></th></span></q></dt></tr></i><div id='h6L2g'><tfoot id='h6L2g'></tfoot><dl id='h6L2g'><fieldset id='h6L2g'></fieldset></dl></div>

              • <bdo id='h6L2g'></bdo><ul id='h6L2g'></ul>
                • <legend id='h6L2g'><style id='h6L2g'><dir id='h6L2g'><q id='h6L2g'></q></dir></style></legend>

                  <small id='h6L2g'></small><noframes id='h6L2g'>

                • <tfoot id='h6L2g'></tfoot>
                  本文介绍了将反射与 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 互操作一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在构建服务器上使用 Microsoft.Office.Interop dll 进行单元测试失败 下一篇:C# 向 COM 公开类 - 通用集合

                  相关文章

                  <i id='JezOy'><tr id='JezOy'><dt id='JezOy'><q id='JezOy'><span id='JezOy'><b id='JezOy'><form id='JezOy'><ins id='JezOy'></ins><ul id='JezOy'></ul><sub id='JezOy'></sub></form><legend id='JezOy'></legend><bdo id='JezOy'><pre id='JezOy'><center id='JezOy'></center></pre></bdo></b><th id='JezOy'></th></span></q></dt></tr></i><div id='JezOy'><tfoot id='JezOy'></tfoot><dl id='JezOy'><fieldset id='JezOy'></fieldset></dl></div>

                  1. <small id='JezOy'></small><noframes id='JezOy'>

                      <bdo id='JezOy'></bdo><ul id='JezOy'></ul>
                    <tfoot id='JezOy'></tfoot>
                    1. <legend id='JezOy'><style id='JezOy'><dir id='JezOy'><q id='JezOy'></q></dir></style></legend>