let's start with the library assembly. use the dialog of figure 2 to create a new library project. this will lead to the following library1 project in the project manager:
as you can see, this is a library project with a reference node that has no references added to it as this time. you can add functionality to this library project by adding a unit to it. as example unit for this article, i want to use the following unit that defined a class trandomnumber that we can "export" from the .net assembly:
unit hardcore.delphi.final.issue;
interface
type
trandomnumber = class
constructor create;
function random10: integer; virtual;
end;
implementation
constructor trandomnumber.create;
begin
inherited;
randomize
end;
function trandomnumber.random10: integer;
begin
result := random(10)
end;
end.
it's only a very simple example, but it will be good enough to demonstrate the way in which we can build and use the two different .net assembly types with delphi 8 for .net. for the library assembly, right-click on the project node and do add, which offers a dialog in which you can select the hardcore.delphi.final.issue.pas unit. now, compile the project, resulting in a library1.dll of 1.3 mbytes. wow! how come? well, take a look at the library source code in library1.dpr and you'll see the following uses clause:
uses
sysutils,
classes,
system.reflection,
hardcore.delphi.final.issue in 'hardcore.delphi.final.issue.pas';
i don't why sysutils and classes are automatically added to the uses clause, but it causes almost the entire vcl for .net to be linked in with the dll assembly. so, to make it a bit smaller, remove the sysutils and classes units from the uses clause and recompile the library1 project. this time the result is a 94 kbyte project1.dll. that's more like it. note that the library project still doesn't have any references added to it in the project manager. this means that the project1.dll assembly doesn't depend on any other .net assemblies, apart from mscorlib and system, which are always present. you can use this library assembly with any development environment like visual studio.net, c#builder, etc. with one exception: you can't add the project1.dll assembly to a delphi 8 for .net application. again, you read it right: the project assembly that we just made with delphi 8 for .net cannot be loaded in any other project we make with delphi 8 for .net.
the reason is simple, but surprising. the fact that the library project contained no external references, resulted in the fact that the borland.delphi.system unit was linked into it. and adding this library assembly to another delphi 8 for .net project, which also contains the borland.delphi.system unit, leads to a fatal error message that says that it could not import assembly 'library1' because it contains namespace 'borland.delphi.system' (which is also included in the new project itself).
according to danny thorpe, the main issue is process-wide global data defined in the system unit.
in order to solve this problem, you need to reload the library assembly project in the ide, and right-click on the library1 project node to add a reference to the borland.system.dll. this way, the library assembly will still use the borland.delphi.system unit, but from an external assembly reference. like a run-time package. and this time, the library1.dll will even be 6.5 kbytes big! now when you add the library1.dll assembly to a delphi 8 for .net project, it will compile just fine, since both the library1.dll assembly and the delphi 8 for .net project will refer to the same external borland.system.dll.
of course, there's one little issue: in order to deploy the library1.dll now, you also need to deploy the borland.system.dll, where previously you could get away with only deploying the library1.dll (in order to use it in visual studio.net or c#builder for example).
one final remark: the delphi 8 for .net ide is a bit sensitive when it comes to building or importing assemblies, and seems to lock them and keep a lock on them, so you can't rebuild an assembly before you close down and restart the ide, and you cannot re-import an assembly correctly either. this problem will most likely be fixed in the upcoming second patch of delphi 8 for .net, but until that time you may want to restart the ide before you start another project anyway.
package