【问题标题】:is there way to track source of call in C# com object?有没有办法在 C# com 对象中跟踪调用源?
【发布时间】:2017-06-02 18:00:48
【问题描述】:

我们有一个从各种电子表格中的 vba 代码调用的 c# com 对象,有没有办法记录调用 com 对象的源?即excel文件名,工作簿等?

【问题讨论】:

    标签: c# excel com vba


    【解决方案1】:

    您可以尝试使用自省来了解调用方法。我不确定这在这种情况下是否有效,因为它是通过 COM 系统调用的,这可能会隐藏起源。无论如何,您都可以尝试以下方法:

    How do I get the calling method name and type using reflection?

    【讨论】:

      【解决方案2】:

      如果您可以同时控制 VBA 代码和 C# 代码,您可以简单地添加一个额外的参数,该参数将被记录为调用 COM 对象的“名称”。然后,您可以在调用它的每个项目中自定义该参数。

      例如。 c#

      [comvisible(true)]
      public void doStuff(string callingName, object otherarg, object anotherarg)
      {
          Console.writeline("Called by " + callingName);
          //blah blah blah
      }
      

      vba 1

      set tmp = ReferenceToCSharpComObject
      call tmp.doStuff("Example VBA Script", null, null)
      

      vba 2

      set tmp = ReferenceToCSharpComObject
      call tmp.doStuff("Second VBA Example", null, null)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-18
        • 2021-03-10
        • 2016-11-21
        • 1970-01-01
        • 1970-01-01
        • 2012-11-01
        • 2018-06-10
        • 1970-01-01
        相关资源
        最近更新 更多