Welcome to ManagedVCL - the best components suite to work with .Net framework from Delphi and C++ Builder. ManagedVCL provides a way to:
- Create .Net objects without registering them with COM.
- Create and use .Net objects that are not COM visible.
- Create .Net objects using parametrized constructors.
- Call properties/methods/fields of .Net object including static properties/methods/fields.
- Handle .Net exceptions and get all .Net exception properties including exception type name, StackTrace etc.
- Get information about loaded instance of .Net framework such as memory usage and size of heap by generation.
Managed VCL can not:
- Query .Net interface if it is not COM visible.
- Work with .Net events.
- Work with .Net controls.
.Net objects
Instance of .Net object is created with one of these functions (declared in ClrUtils unit):
-
//** Creates instance of Assembly.Type in default domain
function ClrCreateInstance(const AssemblyName, TypeName: WideString): OleVariant; overload; -
//** Creates instance of Assembly.Type in specified domain
function ClrCreateInstance(const AssemblyName, TypeName: WideString; domain: _AppDomain): OleVariant; overload; -
//** Create instance of Type in AssemblyFile in default domain
function ClrCreateInstanceFrom(const AssemblyFile, TypeName: WideString): OleVariant; overload; -
//** Create instance of Type in AssemblyFile in specified domain
function ClrCreateInstanceFrom(const AssemblyFile, TypeName: WideString; domain: _AppDomain): OleVariant; overload; -
//** Creates instance of Assembly.Type in default domain
function ClrCreateInstance(const AssemblyName, TypeName: WideString; const Args: OleVariant): OleVariant; overload; -
//** Creates instance of Assembly.Type in specified domain
function ClrCreateInstance(const AssemblyName, TypeName: WideString; const Args: OleVariant; domain: _AppDomain): OleVariant; overload; -
//** Creates instance of Type in AssemblyFile in default domain
function ClrCreateInstanceFrom(const AssemblyFile, TypeName: WideString; const Args: OleVariant): OleVariant; overload; -
//** Creates instance of Type in AssemblyFile in specified domain
function ClrCreateInstanceFrom(const AssemblyFile, TypeName: WideString; const Args: OleVariant; domain: _AppDomain): OleVariant; overload; -
//** Creates instance of Assembly.Type in default domain
function ClrCreateInstance(const AssemblyName, TypeName: WideString; const Args: array of Variant): OleVariant; overload; -
//** Creates instance of Type in AssemblyFile in default domain
function ClrCreateInstanceFrom(const AssemblyFile, TypeName: WideString; const Args: array of Variant): OleVariant; overload;
When creating .Net objects with Managed VCL full type names (including namespace) must be provided, for example, TypeName should be "System.Xml.XmlDocument" but not "XmlDocument". Managed VCL will load .Net framework and needed assembly if needed.
When you create .Net object you get variable of type OleVariant. If .Net object is defined with COM support (ClassInterface attribute is AutoDispatch or AutoDual) you may use it as COM object, i.e.:
procedure Test()
var
obj: OleVariant;
begin
obj := ClrCreateInstance('MyAssembly', 'MyType');
obj.Method(5);
end;
In case of exception this method will raise EOleExcetion, not EClrException. To raise EClrException, use interfaces in .Net classes or TClrObject.ObjectProperty.
Managed VCL includes also special object that lets you call properties/methods/fields more effective - TClrObject. This object lets also work with .Net objects that are not COM visible and with static properties/methods/fields.
.Net types
Managed VCL provides an way to work with .Net types without creating instances of .Net objects. With types you can:
- Call static properties/methods/fields.
- Get information about .Net type: implemented interfaces, properties, methods, fields and events.
.Net type can be created with one of these functions (declared in ClrUtils unit):
-
//** Returns Assembly.Type in default domain
function ClrGetType(const AssemblyName, TypeName: WideString): _Type; overload; -
//** Returns Assembly.Type in specified domain
function ClrGetType(const AssemblyName, TypeName: WideString; domain: _AppDomain): _Type; overload; -
//** Returns Type from Assembly in default domain
function ClrGetTypeFrom(const AssemblyFile, TypeName: WideString): _Type; overload; -
//** Returns Type from Assembly in specified domain
function ClrGetTypeFrom(const AssemblyFile, TypeName: WideString; domain: _AppDomain): _Type; overload;
When calling one of these functions you get pointer to _Type interface that is defined in mscorlib_tlb unit.
Managed VCL contains special object to work with .Net types - TClrType. This object lets you call static properties/methods/fields and get information about .Net type such as enumerate interfaces, constructors, properties, methods, fields, events.
.Net assemblies
Managed VCL can load .Net assemblies using assembly name (for example, "System.Xml, Version = 1.0.5000.0, PublicKeyToken = b77a5c561934e089, Culture = neutral"), assembly file name (for example, "C:\Windows\Microsoft.NET\Framework\v1.1.4322\System.XML.dll"), with assembly partial name (for example, "System.Xml") and even from streams and resources.
Managed VCL includes special component - TClrAssembly that implements all this functionality. This component loads an assembly and can be used to get types and create objects declared in loaded assembly.
Example
To learn more about Managed VCL and try it at work, please start with "Hello dotNet" example. "Embedded assembly" example shows how to compile .Net assembly into Delphi executable (as resource) and load it at runtime.