部分隐藏外部程序集的接口方法

发布于 2024-12-09 02:34:37 字数 418 浏览 0 评论 0原文

我正在寻找将一个接口中的方法“发布”到另一个接口的方法,但对其他人隐藏它。

我有以下接口

public interface IFirst
{
  void Forbidden();
}

public interface ISecond
{
  void Test(IFirst first);
}

internal class Second : ISecond 
{
  void Test(IFirst first)
  {
    first.Forbidden();
  }
}

如您所见,ISecond 的实现需要调用 IFirst 上的“Forbidden”方法。但是,我不想允许另一个程序集中的类在 IFirst 的实现上调用“Forbidden”。我可以做什么来向外界隐藏此方法,但仍允许 ISecond 的实现使用它?

I'm looking for ways to 'publish' a method in one interface to another interface, but hide it for others.

I have the following interfaces

public interface IFirst
{
  void Forbidden();
}

public interface ISecond
{
  void Test(IFirst first);
}

internal class Second : ISecond 
{
  void Test(IFirst first)
  {
    first.Forbidden();
  }
}

As you can see, implementations of ISecond need to call method 'Forbidden' on IFirst. However, I do not want to allow classes in another assembly to call 'Forbidden' on implementations of IFirst. What can I do to hide this method from the outside world but still allowing implementations of ISecond to use it?

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

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

发布评论

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

评论(3

打小就很酷 2024-12-16 02:34:37

Forbidden 方法放入另一个非公共接口中:

public interface IFirst
{
  // whatever ...
}

internal interface IPrivate : IFirst
{
    void Forbidden();
}

public interface ISecond
{
  void Test(IFirst first);
}

internal class Second : ISecond 
{
  void Test(IFirst first)
  {
    var priv = first as IPrivate;
    if (priv != null)
        priv.Forbidden();
  }
}

Put the Forbidden method in another, non-public interface:

public interface IFirst
{
  // whatever ...
}

internal interface IPrivate : IFirst
{
    void Forbidden();
}

public interface ISecond
{
  void Test(IFirst first);
}

internal class Second : ISecond 
{
  void Test(IFirst first)
  {
    var priv = first as IPrivate;
    if (priv != null)
        priv.Forbidden();
  }
}
为你鎻心 2024-12-16 02:34:37

查看内部关键字内部可见属性。将这些结合起来,你应该能够做你想做的事。请注意:这适用于程序集的所有内部。

have a look at the internal keyword and the internalsvisibleto attribute. with those combined you should be able to do what you want. mind you: this applies to all internals of your assembly.

殤城〤 2024-12-16 02:34:37

我认为如果您想要接口,您对此无能为力,就好像您希望 ISecond 的公共方法接受 IFirst 的参数,然后< code>IFirst 必须是公共的,其所有方法也必须是公共的,以便 IFirst 的用户始终可以调用这些方法。

您可以测试 IFirst 的实现,看看它是否实现了另一个内部接口,但除非您能够控制分发正在传递的 IFirst 的实现这不能保证工作(即使您可以控制分发实例,也不能保证工作),因为有人总是可以传入一些不实现内部接口的其他实现。

您也许可以通过使用抽象基类来做到这一点,但我还没有对此进行测试,只是一个想法:

public abstract class FirstBase
{
   protected internal abstract void Forbidden();
}

public interface ISecond
{
  void Test(FirstBase first);
}

internal class Second : ISecond 
{
  void Test(FirstBase first)
  {
    first.Forbidden();
  }
}

I don't think there is anything you can do about this if you want to interfaces, as if you want a public method of ISecond to take a parameter of IFirst then IFirst must be public and so must all its methods, so users of IFirst could always call the methods.

You could test the implementation of IFirst you are given to see if it implements another internal interface, but unless you are in control of dishing out the implementations of IFirst that are being passed in this is not guaranteed to work (and is not guaranteed to work even if you are in control of dishing out the instances) as someone could always pass in some other implementation which doesn't implement the internal interface.

You might be able to do it by using an abstract base class instead, but I haven't tested this, just an idea:

public abstract class FirstBase
{
   protected internal abstract void Forbidden();
}

public interface ISecond
{
  void Test(FirstBase first);
}

internal class Second : ISecond 
{
  void Test(FirstBase first)
  {
    first.Forbidden();
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文