内部构件中的接口 [英] internal member in an interface

查看:165
本文介绍了内部构件中的接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须实现一个接口对象的列表,并且该接口的列表:

I have a list of objects implementing an interface, and a list of that interface:

public interface IAM
{
    int ID { get; set; }
    void Save();
}

public class concreteIAM : IAM
{
     public int ID { get; set; }
     internal void Save(){
     //save the object
     }

    //other staff for this particular class
}

public class MyList : List<IAM>
{
    public void Save()
    {
        foreach (IAM iam in this)
        {
            iam.Save();
        }
    }

    //other staff for this particular class
}

前面的代码不编译因为编译器要求所有的接口成员是公开的。

The previous code doesn't compile because the compiler requires all the interface members to be public.

internal void Save(){

但我不想让从外面我DLL保存 ConcreteIAM ,只应通过 MYLIST

But i don't want to allow the from outside my DLL to save the ConcreteIAM, it only should be saved through the MyList.

任何方式做到这一点。

更新#1 :大家好,感谢您的答案,到目前为止,但没有他们正是我需要的:

Update#1: Hi all, thanks for the answers so far, but none of them is exactly what i need:

该接口必须是公开,因为它是签名从外部客户端的DLL将使用与 ID 等性能我没有理会在这个例子来写,以保持它的简单。

The interface needs to be public because it is the signature the client from outside the dll will use, along with ID and other properties i didn't bother to write in the example to keep it simple.

安德鲁,我不认为解决方案是创建一个工厂来创建另一个对象将包含 IAM 会员+保存。我还在想...任何其他的想法?

Andrew, I don't think the solution is create a factory to create another object that will contain the IAM members + Save. I am still thinking... Any other ideas?

推荐答案

我想你不明白的接口是什么。接口是一个的合同即可。它指定一个物体以某种方式表现。如果一个对象实现一个接口,这意味着你可以依靠它,它有实现的所有接口的方法。

I think you don't understand what an interface is for. Interface is a contract. It specifies that an object behaves in a certain way. If an object implements an interface, it means that you can rely on it that it has all the interface's methods implemented.

现在,考虑是否有一个接口会发生什么像你要求 - 大众,但有一个内部成员。什么会意味着什么?外部对象可以仅实现的公共方法,但不是内部之一。当你会得到这样的外部对象,你就可以只调用的公共方法,而不是内部的,因为对象无法实现它。换句话说 - 在合同就不会得到满足。并不是所有的方法将被执行。

Now, consider what would happen if there was an interface like you're asking for - public, but with one internal member. What would that mean? An external object could implement only the public methods, but not the internal one. When you would get such an external object, you would be able to call only the public methods, but not the internal one, because the object couldn't implement it. In other words - the contract would not be fulfilled. Not all of the methods would be implemented.

我认为在这种情况下,解决办法是分割你的接口两种。一个接口是公开的,这是你的外部对象将执行什么。其他接口将内部和将包含你的保存()和其他内部方法。也许这第二界面甚至可以从第一继承。那么自己的内部对象将实现两个接口。通过这种方式,你甚至可以外部对象(即那些不具备内部接口)和内部区分对象。

I think that the solution in this case is to split your interface in two. One interface would be public, and that's what your external objects would implement. The other interface would be internal and would contain your Save() and other internal methods. Perhaps this second interface could even inherit from the first. Your own internal objects would then implement both interfaces. This way you could even distinguish between external objects (ones that don't have the internal interface) and internal objects.

这篇关于内部构件中的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆