确保封装的建议

发布于 2024-11-25 16:33:10 字数 929 浏览 1 评论 0原文

在我的解决方案中,我有一个 DLL 项目(例如 Project1),它拥有一个类(例如 class A)。该类有一些非常重要的属性,只能在该 DLL 中设置。引用此 DLL 的任何其他项目只能使用 getter。到目前为止,我已经通过内部访问设置器来处理这种情况,例如:

public class A
{
  int Num1 
  {
    get; 
    internal set;
  }

  int Num2 
  {
    get; 
    internal set;
  }
} 

现在我必须将另一个项目 Project2 添加到解决方案中,该解决方案不仅必须使用 class A (getter 和 setter)而且,Project1 还引用了这个新项目。因此,我决定将 A 类 分离到层次结构顶部的不同定义 dll 中。这就是我的问题所在。现在,所有 setter 对于 Project1Project2 都是不可见的。如果我删除 internal 访问器,那么引用定义 dll 的任何程序集都可以使用 setter。我不想这样,因为class A的信息非常重要,一定不能设置错误。

如何限制从 Project1Project2 外部对 class A 的 setter 的访问?

我想到的唯一解决方案是使用InternalsVisibleTo。我不知道,听起来并不完美。另外,将 Project1Project2 结合起来也是另一种解决方案,但这两个解决方案负责完全不同的任务,这不是设计方面的最佳选择。

In my solution I have a DLL project, say Project1, that owns a class, say class A. This class has some properties, so vital that must only be set in this DLL. Any other projects that references this DLL can only use the getters. So far I have handled the situation by accessing the setters internally, like:

public class A
{
  int Num1 
  {
    get; 
    internal set;
  }

  int Num2 
  {
    get; 
    internal set;
  }
} 

Right now I have to add another project, Project2, to the solution that not only must use class A (both getters and setter) but also, Project1 has a reference to this new project. So I decided to separate class A in a different definition dll, on the top of the hierarcy. That is the point, my problem araises. Now all the setters are invisible to Project1 and Project2. If I remove the internal accessor then setters will be available to any assembly that references the definition dll. I do not want this because the information on the class A is so important, it must not be set mistakenly.

How can I limit the access to the setters of class A from outside of Project1 and Project2?

Only solution comes to my mind is using InternalsVisibleTo. I do not know, it doesn't sound perfect. Also combining Project1 and Project2 would be another another solution, but these two are responsible for completely different tasks, it is not the best option designwise.

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

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

发布评论

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

评论(3

挽心 2024-12-02 16:33:10

您可以将内部保护器保留在其上,但给予另一个程序集访问内部成员的信任。

[assembly: InternalsVisibleTo("AssemblyName")]

您可以在类级别执行此操作,如果需要,也可以在整个程序集本身上执行此操作。

InternalsVisibleToAttribute 类

You can keep the internal protector on it, but give another assembly the trust to access the internal members.

[assembly: InternalsVisibleTo("AssemblyName")]

You can do this at the class level, or on the entire assembly itself if you want.

InternalsVisibleToAttribute Class

地狱即天堂 2024-12-02 16:33:10

如果我正确地假设 Project1 执行一些特定的操作,而 Project2 是其之上的抽象,那么这是控制反转的候选者 - 理想情况下,实现应该取决于在抽象上,而不是相反。

如果 Project2 中的内容需要控制 Project1 中定义的内容,请在 Project2 中创建一个接口并让 Project2 与该接口通信,然后在 Project1 中实现该接口并在内部完成所有脏工作。

If I'm right in assuming that Project1 does some specific stuff, and Project2 is an abstraction on top of that, then this is a candidate for inversion of control - ideally, an implementation should depend on an abstraction, not the other way around.

If things within Project2 need to control something defined in Project1, create an interface in Project2 and have Project2 talk to the interface, then implement the interface in Project1 and do all the dirty work internally.

绝不服输 2024-12-02 16:33:10

你总是可以做这样的事情(设计的噩梦,但做了你要求的事情):

public class A
{
  int _Num1, _Num2;

  int Num1 {get {return _Num1;}; }
  int Num2 {get {return _Nun2;}; }

  protected void SetNum (int pWhich, int pV)
  {
      if ( pWhich == 1 ) {_Num1 = pV; return;}
      _Num2 = pV;
  } 
} 

然后你可以正常实例化类......当你需要设置使用反射的值时:

myClassA.GetType().GetMethod ("SetNum", BindingFlags.NonPublic ).Invoke (myClassA, new object[] {1, 777 });

you could always do something like this (a nightmare of a design but does what you asked for):

public class A
{
  int _Num1, _Num2;

  int Num1 {get {return _Num1;}; }
  int Num2 {get {return _Nun2;}; }

  protected void SetNum (int pWhich, int pV)
  {
      if ( pWhich == 1 ) {_Num1 = pV; return;}
      _Num2 = pV;
  } 
} 

and then you could instanciate the class normally... when you need to set the values you use reflection:

myClassA.GetType().GetMethod ("SetNum", BindingFlags.NonPublic ).Invoke (myClassA, new object[] {1, 777 });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文