简体   繁体   English

如何不将 class 暴露给任何其他 class 而不是一个? (C#)

[英]how to not expose a class to any other class but one? (C#)

I have a namespace ABC, which has one form class, F and an ordinary class C.我有一个命名空间 ABC,它有一种形式 class、F 和一个普通的 class C。 Now I have another namespace XYZ with many different classes.现在我有了另一个名称空间 XYZ,其中包含许多不同的类。 Now, F is used only from C and nowhere else in the program.现在,F 仅在 C 中使用,在程序中没有其他地方使用。 Every other classes use C, not F.其他所有类都使用 C,而不是 F。

In visual studio (2010), from classes in XYZ namespace I can see F listed in the intellisense dropdown (since I've referenced ABC to use C).在 Visual Studio (2010) 中,从 XYZ 命名空间中的类中,我可以看到智能感知下拉列表中列出的 F(因为我已经引用 ABC 来使用 C)。 How can I prevent that?I want only C there.我该如何防止呢?我只想要 C 那里。 Any method would suffice as long as I can use C from XYZ.只要我可以使用来自 XYZ 的 C,任何方法都足够了。

Thanks.谢谢。

Can you make F a nested class inside of your C class?你能在你的 C class 内部制作一个嵌套的 class 吗?

public class C
{
    public C()
    { }

    class F : Form
    {
        ...
    }
}

I'm not sure if this works for your scenario, but it would solve the Intellisense problem, while satisfying the requirements listed above.我不确定这是否适用于您的场景,但它可以解决 Intellisense 问题,同时满足上面列出的要求。

Is there any reason why you can't put it in another namespace to hide it from the other classes, eg namespace ABC.Forms?有什么理由不能将它放在另一个命名空间中以对其他类隐藏它,例如命名空间 ABC.Forms?

If it were anything other than a form class (therefore is a partial class if you are using a recent version of .net) - I would say, why not make it an inner class to C - but I doubt that would be neat with a form class. If it were anything other than a form class (therefore is a partial class if you are using a recent version of .net) - I would say, why not make it an inner class to C - but I doubt that would be neat with a形成 class。

I would go with the namespace change.我会 go 更改命名空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM