Pregunta

What is the scope of a friend assembly statement?

Example:

A.cs
[assembly: InternalVisibleTo("Friend")]
internal class A { ... }

B.cs
internal class B { ... }

Will class B be accessible for an assembly called "Friend"?

Is the scope the whole project (even the assembly statement is only set in one single class)?

¿Fue útil?

Solución

The attribute is not applied to the class, this is because the keyword assembly: stands before it. This means the attribute is applied to the whole assembly.

As you can see on the MSDN page. The declaration of the attribute specifies that it can only be applied to assemblies. And therefor makes every internal in the assembly visible to the "friend" assembly.

Otros consejos

You can use the InternalsVisibleToAttribute attribute to identify one or more friend assemblies for a given assembly. The following example uses the InternalsVisibleToAttribute attribute in assembly A and specifies assembly AssemblyB as a friend assembly. This gives assembly AssemblyB access to all types and members in assembly A that are marked as Friend (Visual Basic) or internal (C#).

Taken from msdn

You can see InternalsVisibleToAttribute is defined as below. Note the AttributeTargets allows only for assembly not for Types.

[AttributeUsageAttribute(AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
public sealed class InternalsVisibleToAttribute : Attribute

It doesn't matter where you place this attribute, it makes all the types which are internal in assembly visible to the friend assembly.

B would be accessible to your friend assembly. SynerCoder beat me to the post, but as he stated, the statement applies at the assembly level.

This is useful for getting your Test projects access to your internal methods and types :)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top