Friday, October 01, 2004

Assembly <> Namespace 

One thing that some Delphi developers getting their feet wet in the .NET pool can have fun coming to grips with is the concept of namespaces. In a nutshell, a namespace provides the ability to group related types together in a 'container'. It is important to note however that there is not a 1 to 1 correlation between namespaces and assemblies, and the namespace name might not accurately reflect the assembly where the namespace resides. In addition to this, a namespace may be implemented in multiple assemblies. A more exhaustive explanation can be found at MSDN.

This was highlighted today when helping to convert a C# snippet to Delphi for another developer. One of the classes in the code resided in the System.Windows.Forms.Design namespace, but was actually implemented in the System.Design.dll assembly. As a result, the Delphi unit needed to use System.Windows.Forms.Design in its uses section in order to access the class, but the project also needed reference the System.Design.dll in order to resolve the symbol information for the type. In this case, it was slightly confusing due to the existance of a System.Windows.Forms.dll assembly. It is not illogical for the .NET newcomer to think that the System.Windows.Forms.Design namespace would be implemented in the System.Windows.Forms.dll assembly.

Thankfully the .NET FCL documentation does an excellent job of helping out here. When browsing details for a specific type, it will usually (I say usually, but am yet to encounter where it doesn't) tell you the namespace where the type is declared (needed for the uses section), and the assembly where it resides (needed for the project reference).

Unfortunately, in Delphi 8, we don't have the ability to have multiple units contributing to the same name space. In Delphi 8 the namespace is constructed from the unit filename. This means that if you want to have a bunch of types contained in the same namespace, they must reside in the same unit. However, one of the things to come out of the DiamondBack previews at BorCon this year is that the next version of Delphi will support multi-unit namespaces.

Comments: Post a Comment

This page is powered by Blogger. Isn't yours?