什么是朋友的 C# 等价物?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/204739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:56:26  来源:igfitidea点击:

What is the C# equivalent of friend?

c#

提问by Ben L

Possible Duplicate:
Why does C# not provide the C++ style ‘friend' keyword?

可能的重复:
为什么 C# 不提供 C++ 风格的“朋友”关键字?

I'd like the private member variables of a class to be accessible to a Tester class without exposing them to other classes.

我希望 Tester 类可以访问类的私有成员变量,而不会将它们暴露给其他类。

In C++ I'd just declare the Tester class as a friend, how do I do this in C#? Can someone give me an example?

在 C++ 中,我只是将 Tester 类声明为朋友,我该如何在 C# 中做到这一点?有人可以举个例子吗?

采纳答案by Jon Skeet

There's no direct equivalent of "friend" - the closest that's available (and it isn't very close) is InternalsVisibleTo. I've only ever used this attribute for testing - where it's very handy!

没有“朋友”的直接等价物 - 可用的最接近的(并且不是非常接近)是InternalsVisibleTo。我只使用过这个属性进行测试 - 它非常方便!

Example:To be placed in AssemblyInfo.cs

示例:放置在AssemblyInfo.cs

[assembly: InternalsVisibleTo("OtherAssembly")]

回答by RobG

There isn't a 'friend' keyword in C# but one option for testing private methods is to use System.Reflection to get a handle to the method. This will allow you to invoke private methods.

C# 中没有“friend”关键字,但测试私有方法的一种选择是使用 System.Reflection 来获取该方法的句柄。这将允许您调用私有方法。

Given a class with this definition:

给定一个具有此定义的类:

public class Class1
{
    private int CallMe()
    {
        return 1;
    }
}

You can invoke it using this code:

您可以使用以下代码调用它:

Class1 c = new Class1();
Type class1Type = c.GetType();
MethodInfo callMeMethod = class1Type.GetMethod("CallMe", BindingFlags.Instance | BindingFlags.NonPublic);

int result = (int)callMeMethod.Invoke(c, null);

Console.WriteLine(result);

If you are using Visual Studio Team System then you can get VS to automatically generate a proxy class with private accessors in it by right clicking the method and selecting "Create Unit Tests..."

如果您使用的是 Visual Studio Team System,那么您可以通过右键单击该方法并选择“创建单元测试...”,让 VS 自动生成带有私有访问器的代理类。

回答by Freddy mac

Take a very common pattern. Class Factory makes Widgets. The Factory class needs to muck about with the internals, because, it is the Factory. Both are implemented in the same file and are, by design and desire and nature, tightly coupled classes -- in fact, Widget is really just an output type from factory.

采取一个非常常见的模式。类工厂制作小部件。Factory 类需要处理内部结构,因为它是 Factory。两者都在同一个文件中实现,并且根据设计、愿望和性质,是紧密耦合的类——事实上,Widget 只是工厂的输出类型。

In C++, make the Factory a friend of Widget class.

在 C++ 中,使工厂成为 Widget 类的朋友。

In C#, what can we do? The only decent solution that has occurred to me is to invent an interface, IWidget, which only exposes the public methods, and have the Factory return IWidget interfaces.

在 C# 中,我们可以做什么?我想到的唯一合适的解决方案是发明一个接口 IWidget,它只公开公共方法,并让工厂返回 IWidget 接口。

This involves a fair amount of tedium - exposing all the naturally public properties again in the interface.

这涉及相当多的乏味——在界面中再次暴露所有自然公共属性。

回答by sjp

The closet equivalent is to create a nested class which will be able to access the outer class' private members. Something like this:

壁橱等效于创建一个嵌套类,该类将能够访问外部类的私有成员。像这样的东西:

class Outer
{
    class Inner
    {
       // This class can access Outer's private members
    }
}

or if you prefer to put the Inner class in another file:

或者如果您更喜欢将内部类放在另一个文件中:

Outer.cs
partial class Outer
{
}


Inner.cs
partial class Outer
{
    class Inner
    {
       // This class can access Outer's private members
    }
}

回答by smartmobili

You can simulate a friend access if the class that is given the right to access is inside another package and if the methods you are exposing are marked as internal or internal protected. You have to modify the assembly you want to share and add the following settings to AssemblyInfo.cs :

如果被授予访问权限的类在另一个包中,并且您公开的方法被标记为内部或内部保护,则您可以模拟朋友访问。您必须修改要共享的程序集并将以下设置添加到 AssemblyInfo.cs :

// Expose the internal members to the types in the My.Tester assembly
[assembly: InternalsVisibleTo("My.Tester, PublicKey=" +
"012700000480000094000000060200000024000052534131000400000100010091ab9" +
"ba23e07d4fb7404041ec4d81193cfa9d661e0e24bd2c03182e0e7fc75b265a092a3f8" +
"52c672895e55b95611684ea090e787497b0d11b902b1eccd9bc9ea3c9a56740ecda8e" +
"961c93c3960136eefcdf106955a4eb8fff2a97f66049cd0228854b24709c0c945b499" +
"413d29a2801a39d4c4c30bab653ebc8bf604f5840c88")]

The public key is optional, depending on your needs.

公钥是可选的,具体取决于您的需要。