基类中的 COM 对象 - 通过字段或属性访问?

发布于 2024-08-18 16:45:39 字数 1137 浏览 4 评论 0原文

我继承了通过 COM 互操作调用的 C# dll 代码库(或者已经描述过)。 C# 代码还在内部使用 COM 对象来执行父应用程序的基本功能。

我正在重构代码中的一些 DRY 违规行为,因为在 50 或 60 个 dll 中查找 100,000 行代码中的重复项效率很低。我在抽象基类中遇到了 COM 对象的使用,我想对其进行一些标准化,但我还没有在任何地方找到关于 C# 中 COM 对象的这种特定使用的明确定义。

我们的代码当前有几个包含 COM 对象的基类,编码如下:

public abstract class SomeBaseClass()
{
   protected IComObject comObject;

   protected virtual void Initialize(IComObject comObject)
   {
      this.comObject = comObject;
   }

   protected SomeBaseClass() { }
}

为了防止 this.comObject 在 Initialize() 之外的其他方式中设置,我想像这样实现这些基类:

public abstract class SomeBaseClass()
{
   private IComObject comObject;

   protected IComObject ComObject
   {
      get { return comObject; }
   }

   protected virtual void Initialize(IComObject comObject)
   {
      this.comObject = comObject;
   }

   protected SomeBaseClass() { }
}

在我看来,第二个示例看起来更好,让我可以更好地控制内部 comObject。

目前,现有的派生类(C#)并不直接设置基类comObject,而是使用Initialize(),但没有什么可以阻止它们直接分配给基类comObject。我想防止将来在 Initialize() 之外分配给 comObject 的潜在错误。

是否有某种原因导致 COM 对象处理的第二个基类实现无法工作?在我有限的测试中,它似乎工作得很好,但你们比我聪明。

谢谢!

I've inherited a codebase of C# dlls that are called via COM-interop (or so it has been described). The C# code also uses COM objects internally to perform the basic functionality of the parent application.

I'm refactoring some of the DRY violations out of the code because finding duplications in 100,000 lines of code across 50 or 60 dlls is inefficient. I've come across a use of COM objects in abstract base classes that I'd like to standardize a bit, but I haven't found a clearly definitive statement anywhere about this particular use of COM objects in C#.

Our code currently has several base classes that contain COM objects, coded like this:

public abstract class SomeBaseClass()
{
   protected IComObject comObject;

   protected virtual void Initialize(IComObject comObject)
   {
      this.comObject = comObject;
   }

   protected SomeBaseClass() { }
}

To prevent this.comObject from being set in other than Initialize(), I'd like to implement these base classes like this:

public abstract class SomeBaseClass()
{
   private IComObject comObject;

   protected IComObject ComObject
   {
      get { return comObject; }
   }

   protected virtual void Initialize(IComObject comObject)
   {
      this.comObject = comObject;
   }

   protected SomeBaseClass() { }
}

In my opinion the second example looks better and gives me more control over the internal comObject.

Currently, the existing derived classes (C#) do not set the base class comObject directly, instead using Initialize(), but there is nothing to prevent them from assigning to the base class comObject directly. I want to prevent the potential future mistake of assigning to comObject outside of Initialize().

Is there some reason that the second base class implementation of COM object handling will not work? In my limited testing it seems to work fine, but you guys are smarter than I am.

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一指流沙 2024-08-25 16:45:39

涉及 COM Interop 的任何内容都不会导致第二种情况与第一种情况有任何不同。请随意将该字段的访问修饰符调整为私有。

There is nothing involving COM Interop that would make the second case any different than the first. Feel free to adjust the access modifier of the field to private.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文