Does C # 4 optimize the namespace so that previous versions of C # did not?

This question is for interest. I work with a third-party library and stumbled upon the following documentation in the CMS.Security.Dummy class:

DO NOT DELETE THIS CLASS. This class prevents the compiler from deleting the entire namespace in .NET 4.0.

Does anyone know, or can anyone suggest, why .NET 4 will reset the namespace if the remote class was deleted?

Since .NET 4 is explicitly referenced in the source code comment, I assume that previous versions of C # demonstrate behavior that does not require this dummy class. This is purely speculative.

Screenshot

documentation

Decompiled source code

 #region Assembly CMS.SettingsProvider.dll, v4.0.30319 // ...\solution\wwwroot\Bin\CMS.SettingsProvider.dll #endregion using System; namespace CMS.Security { // Summary: // DO NOT DELETE THIS CLASS - This class prevents the compiler from dropping // entire namespace under .NET 4.0. public class Dummy { // Summary: // DO NOT DELETE THIS CLASS - This class prevents the compiler from dropping // entire namespace under .NET 4.0. public Dummy(); } } 
+44
c # namespaces
Mar 05 2018-12-21T00:
source share
3 answers

It is clear that there is no such thing as a “namespace” from the point of view of a base system such as the CLR. Rather, it is simply an agreement that we say that a type containing periods in its name is a "member of the namespace". Logically, there is no difference between the legal code:

 namespace N { class C {} } 

and psuedo-code:

 class NC {} 

C # makes you pretend that this nice fantasy is real, but it's just a fiction - from the point of view of a CLR system, of course. From the point of view of the C # compiler, of course, namespaces are "real." They just don't match anything in the metadata except for the part of the type name.

In short: if you create an assembly with an "empty" namespace, then the "namespace" does not exist at all in the compiled binary. A “namespace” occurs only when there is a type in the library that has periods in its name.

Now, why do you care that the "empty" namespace has some presence in binary form, I have no idea.

I assume previous versions of C # demonstrate behavior that does not require this dummy class

Nope. Each version of C # with 1.0 throws out empty namespaces.

+90
Mar 05 '12 at 22:22
source share
— -

Given that the namespace does not contain any members (without this class), I'm not sure that even the concept of the namespace at this point ... and I would not expect this to be useful anyway.

I just tried to reproduce this using the C # 2 compiler, and I don't see a single trace of the empty namespace inside IL.

+23
Mar 05 '12 at 21:45
source share

The only semi-dependency problem that I can come up with is that when compiling the project in msbuild, indirect links are not always copied to the bin directory of the current application. If library B indirectly refers only to library A and the library of the C-reference B, then the library C will not be copied to the bin folder during assembly. In the past, I used a null field reference for a class to make sure the dependency is explicit and the output is deployed correctly. Perhaps the original developers experienced something similar, and this was their solution?

+7
Mar 05 '12 at 21:50
source share



All Articles