使特定的内部功能对另一个组件可见(即并非所有内部都可见)

时间:2014-04-02 16:18:55

标签: c# internal internalsvisibleto

可以在AssemblyInfo.cs中指定[assembly: InternalsVisibleTo("NameOfOtherAssembly")]

但是可以将其限制为特定的内部功能吗?

是否有例如一个可以应用于每个函数的属性?

[InternalVisibleTo("NameOfOtherAssembly")]
internal void ShouldBeVisible()
{}

internal void ShouldNotBeVisible()
{}

如果没有,还有其他方法可以实现吗?

1 个答案:

答案 0 :(得分:-1)

The attribute is applied at the assembly level. 
This means that it can be included at the beginning of a source code file, 
or it can be included in the AssemblyInfo file in a Visual Studio project. 
You can use the attribute to specify a single friend assembly that can access 
the internal types and members of the current assembly.

您可以在源代码文件 的开头添加 属性。您无法使用它标记单个方法,请参阅MSDN - Documentation

如果要分割类的可见和不可见方法,可以执行以下操作:

在一个文件中,定义可见方法并将文件标记为可见:

InternalsVisibleTo("NameOfOtherAssembly")]
public partial class Foo
{
  internal void Visible(){}
}

在另一个文件中,定义不可见的方法:

public partial class Foo
{
  internal void IsNotVisible(){}
}