RAD Studio
ContentsIndex
PreviousUpNext
.NET Components and Type Libraries

Both COM, and the .NET Framework contain mechanisms to expose type information. In COM, one such mechansim is the type library. Type libraries are a binary, programming language-neutral way for a COM object to expose type metadata at runtime. Because type libraries are opened and parsed by system APIs, languages such as Delphi can import them and gain the advantages of vtable binding, even if the component was written in a different programming language. 

In the .NET development environment, the assembly doubles as a container for both IL, and type information. The .NET Framework contains classes that are used to examine (or, "reflect") the types contained in an assembly. When you access a .NET component from unmanaged code, you are actually using a proxy (the COM Callable Wrapper, mentioned earlier), not the .NET component itself. The CCW mechanism, plus the self-describing nature of assemblies, is enough to allow you to access a .NET component entirely through late binding. 

Because you can access a .NET component through late binding, creating a type library for the component is not strictly required. All that is required is that the assembly be registered. In fact, unmanaged clients are restricted to late binding by default. Depending on how the .NET component was designed and built, you might find only an "empty" class interface if you inspect its type library. Such a type library is useless, in terms of enabling clients to use vtable binding instead of late binding through IDispatch

The following example demonstrates how to late bind to the ArrayList collection class contained in mscorlib.dll. The mscorlib assembly must be registered prior to using any type in the manner described here. The Delphi installer automatically registers mscorlib, but you can run the regasm utility again if need be (e.g. you unregistered mscorlib with the /u regasm option). Execute the command

regasm mscorlib.dll

in the .NET Framework directory to register the mscorlib assembly.

Note: Do not use the /tlb option when registering mscorlib.dll. The .NET Framework already includes a type library for the mscorlib assembly; you do not need to create a new one.
The following code is attached to a button click event of a Delphi form:

procedure TForm1.Button1Click(Sender: TObject);
var
   capacity: Integer;
   item:Variant;
   dotNetArrayList:Variant;
begin
   { Create the object }
   dotNetArrayList := CreateOleObject('System.Collections.ArrayList');

   { Get the capacity of the ArrayList }
   capacity := dotNetArrayList.Capacity;

   { Add an item }
   dotNetArrayList.Add('A string item');

   { Retrieve the item, using the Array interface method, Item(). }
   item := dotNetArrayList.Item(0);

   {Remove all items }
   dotNetArrayList.Clear;
end;
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!