• <legend id='tHulu'><style id='tHulu'><dir id='tHulu'><q id='tHulu'></q></dir></style></legend>

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

        <bdo id='tHulu'></bdo><ul id='tHulu'></ul>
      <i id='tHulu'><tr id='tHulu'><dt id='tHulu'><q id='tHulu'><span id='tHulu'><b id='tHulu'><form id='tHulu'><ins id='tHulu'></ins><ul id='tHulu'></ul><sub id='tHulu'></sub></form><legend id='tHulu'></legend><bdo id='tHulu'><pre id='tHulu'><center id='tHulu'></center></pre></bdo></b><th id='tHulu'></th></span></q></dt></tr></i><div id='tHulu'><tfoot id='tHulu'></tfoot><dl id='tHulu'><fieldset id='tHulu'></fieldset></dl></div>
      <tfoot id='tHulu'></tfoot>
      1. 在 .NET COM 互操作中传递强类型参数

        时间:2023-07-24

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

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

                • <bdo id='uKn5i'></bdo><ul id='uKn5i'></ul>
                    <tbody id='uKn5i'></tbody>
                  本文介绍了在 .NET COM 互操作中传递强类型参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有两个通过 COM 互操作公开的 .NET 类 - 比如说 Foo 和 Bar,我需要将 Foo 类型的参数传递给 Bar 中定义的方法.像这样的:

                  I have two .NET classes exposed via COM interop - let's say Foo and Bar, and I need to pass an argument of type Foo to a method defined in Bar. Something like this:

                  [ComVisible(true)]
                  public class Foo
                  {
                      // whatever
                  }
                  
                  [ComVisible(true)]
                  public class Bar
                  {
                      public void Method(Foo fff)
                      {
                          // do something with fff
                      }
                  }
                  

                  当我运行以下 VBS(使用 cscript.exe)时:

                  When I run the following VBS (using cscript.exe):

                  set foo = CreateObject("TestCSProject.Foo")
                  set bar = CreateObject("TestCSProject.Bar")
                  call bar.Method(foo)
                  

                  我得到一个错误:

                  D: est.vbs(3, 1) Microsoft VBScript 运行时错误:无效的过程调用或参数:'bar.Method'

                  但是,如果我将方法声明更改为:

                  However, if I change the Method declaration to this:

                      public void Method(object o)
                      {
                          Foo fff = (Foo)o;
                          // do something with fff
                      }
                  

                  一切正常.我尝试了一些关于接口、属性等的魔法,但到目前为止没有运气.

                  everything works. I tried some magic with interfaces, attributes, etc. but no luck so far.

                  有什么见解吗?

                  非常感谢

                  推荐答案

                  确保你定义了一个 GUID 属性,如果你创建一个 QueryInterface(VB 可能会这样做),这是必要的.您必须为每个可组合的类生成一个新的唯一 GUID.

                  Make sure, you define a GUID attribute, this is necessary if you make a QueryInterface (VB does probably). You have to generate a new unique GUID for every comvisible class.

                  [Guid("77777777-3333-40df-9C0D-2B580E7E1F3B")]
                  [ComVisible(true)]
                  public class Foo
                  {
                  }
                  

                  然后我强烈建议为您的 COM 对象编写接口,并将 ClassInterface 设置为 None,因此不会显示任何内部结构.这样你的类型库会更干净.

                  Then i would strongly recommend to write interfaces for your COM objects, and set the ClassInterface to None, so no internals are revealed. Your typelibrary will be much cleaner this way.

                  [Guid("88888888-ABCD-458c-AB4C-B14AF7283A6B")]
                  [ComVisible(true)]
                  public interface IFoo
                  {
                  }
                  
                  [ClassInterface(ClassInterfaceType.None)]
                  [Guid("77777777-3333-40df-9C0D-2B580E7E1F3B")]
                  [ComVisible(true)]
                  public class Foo : IFoo
                  {
                  }
                  

                  这篇关于在 .NET COM 互操作中传递强类型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:通过 C# 启动的 PowerPoint 不退出 下一篇:如何让 LINQPad 转储() System.__ComObject 引用?

                  相关文章

                • <tfoot id='eFhUZ'></tfoot>

                  <legend id='eFhUZ'><style id='eFhUZ'><dir id='eFhUZ'><q id='eFhUZ'></q></dir></style></legend>
                • <small id='eFhUZ'></small><noframes id='eFhUZ'>

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