0

在以下情况下(请原谅我的伪代码):

Assembly Lib:
   Interface A;
   Class 123; //Utilizes multiple properties with a { get; internal set; } pattern.

   //Contains more class definitions like Class 123

Assembly Impl: //References Lib 
   Class MyImpl; //Implements Interface A and utilizes Class 123.


ClientApplication:
   //Consumes Interface A

我想知道是否有一种方法可以让 MyImpl 类访问 Class 123 的属性 set() 而不使用静态声明的友元程序集,并使这些 set() 也对 ClientApplication 隐藏。

最终目标是能够以允许 ClientApplication 使用来自 AssemblyLib 的类型的方式部署它,并且能够删除新的实现程序集,而无需重新编译和重新分发 Assembly Lib。

我正在考虑寻找一种方法来使内部修饰符在其实例化(Assembly Impl)而不是在其声明(Assembly Lib)时生效,但我不相信 C# 支持这样的东西。

我意识到如果可以做到,这可能真的很丑陋,而且我不确定自己是否可以做到,但这就是我在这里的原因!提前感谢您的帮助。

4

2 回答 2

2
  1. 您可以使用反射来访问 Lib 的内部。这不是一个很好的解决方案。
  2. 您可以使 Impl 成为 Lib 的朋友程序集。这不是一个很好的解决方案。
  3. 您可以重组您的设计以解决 Impl 访问 Lib 内部的需求。这是一个很好的解决方案。

使用您的示例,这是我要了解的内容。为什么不将设计更改为:

Assembly Lib:
   Interface A;
   Interface B; //Utilizes multiple properties with a { get; } pattern.

   //Contains more interface definitions like Interface B

Assembly Impl: //References Lib 
    Class 123; //Utilizes multiple properties with a { get; internal set; } 
               //pattern. Implements B.

    //Contains more class definitions like Class 123

    Class MyImpl; //Implements Interface A and utilizes Class 123.

ClientApplication:
    //Consumes implementations of Interface A and B provided by Impl.
于 2013-10-15T14:49:03.380 回答
0

你可能想看看InternalsVisibleToAttribute这个。

例如,在您的Lib程序集中,将此行添加到您的assemblyinfo.cs文件中:

[assembly:InternalsVisibleTo("Impl")]
于 2013-10-15T14:48:09.883 回答