Search code examples
c#.netcompilation.net-assemblyfriend

Why does compiler need the assembly file name when compiling the friend assembly?


Why does compiler need the assembly file name when compiling the friend assembly?

I am reading the CLR via C# book and I come across the following excerpt:

Note that the C# compiler requires you to use the /out: compiler switch when compiling the friend assembly (the assembly that does not contain the InternalsVisibleTo attribute). The switch is required because the compiler needs to know the name of the assembly being compiled in order to determine if the resulting assembly should be considered a friend assembly. You would think that the C# compiler could determine this on its own since it normally determines the output file name on its own; however, the compiler doesn’t decide on an output file name until it is finished compiling the code. So requiring the /out: compiler switch improves the performance of compiling significantly.

Also, if you are compiling a module (as opposed to an assembly) using C#’s /t:module switch, and this module is going to become part of a friend assembly, you need to compile the module by using the C# compiler’s /moduleassemblyname: switch as well. This tells the compiler what assembly the module will be a part of so the compiler can allow code in the module to access the other assembly’s internal types.

The excerpt above means that the file in which an assembly is put and the assembly have the same name. But that is not the case, because a strongly named assembly name consists of assembly's name, culture, public key, and version number.

Otherwise, I come to a conclusion that it is impossible to give a definition to the assembly name, because the definition of the name assumes that we already know what the name is. Because name = name + culture + public key + version number.

So, could someone explain in a foolproof manner why does the file name matters for a friend assembly compilation? And what is the relation between the file and assembly names?


Solution

  • Why does compiler need the assembly file name when compiling the friend assembly?

    It's stated in the quotation you supplied:

    You would think that the C# compiler could determine this on its own since it normally determines the output file name on its own; however, the compiler doesn’t decide on an output file name until it is finished compiling the code. So requiring the /out: compiler switch improves the performance of compiling significantly.

    OP:

    The excerpt above means that the file in which an assembly is put and the assembly have the same name. But that is not the case, because a strongly named assembly name consists of assembly's name, culture, public key, and version number.

    It doesn't say that at all and that is not the definition of a strongly named (SN) assembly.

    MSDN:

    A strong name consists of the assembly's identity — its simple text name, version number, and culture information (if provided) — plus a public key and a digital signature. More....

    Sure the simple text name may be derived from the assembly/filename at the time it was signed but that is incidental.

    OP:

    Otherwise, I come to a conclusion that it is impossible to give a definition to the assembly name, because the definition of the name assumes that we already know what the name is. Because name = name + culture + public key + version number.

    Strong-naming (I assume we are still on that point) an assembly occurs after the assembly has been generated. You cannot sign that that does not exist. If it exists, it has a simple text name.

    Also, why are we now talking about .NET strong naming (SN) of assemblies?