Generated code
An overview of the C# code that CsCom generates from a type library, for this use of Scripting.Dictionary. The other
articles of this section describe the details.
IDictionary dictionary = Dictionary.Create(); // coclass: CoCreateInstance + ComWrappers
dictionary.CompareMode = CompareMethod.TextCompare; // C# 14 extension property over set_CompareMode
dictionary.Add("a", 1); // VARIANT parameters are object?
Console.WriteLine(dictionary.Count); // C# 14 extension property over get_Count
foreach (object? key in dictionary) // _NewEnum via IEnumVARIANT
{
}
- Interfaces and dual interfaces become
[GeneratedComInterface]interfaces. HRESULTs are translated to exceptions and[retval]parameters to return values, except a[retval] void*, which stays a parameter; methods that do not return an HRESULT get[PreserveSig]. Dual interfaces derive fromIDispatch, and interfaces derived fromIEnumVARIANTfromIEnumVARIANT, so that the vtable slots match. An interface without a GUID cannot be generated: the pointers to it areIUnknownorIDispatchpointers, and the interfaces that derive from it get placeholders for its vtable slots. - Names: invalid characters become
_, and a name that clashes gets a trailing_: a member named like another member or like its type, and a generated type (<Interface>Extensions, the types of the events) named like a type of the library. - Pointer parameters are
out([out]),ref([in, out]) or, for[in]pointers toVARIANTs,SAFEARRAYs, structures and GUIDs,inparameters. A pointer without a direction, as old type libraries have them, is arefparameter. - Properties cannot be declared by source-generated COM interfaces, so they are generated as
get_X,set_X(propput) andputref_X(propputref) methods. A static<Interface>Extensionsclass adds C# 14 extension properties for them and aGetEnumeratorextension for collections. With C# 15, the default parameterized property (DISPID_VALUE, usuallyItem) becomes an extension indexer. The extensions of a derived interface also use the accessors of its base interfaces, e.g. the getter of a read-only property ofIMyInterfaceand the setter thatIMyInterface2adds to it. The other parameterized properties can get indexer syntax too, see Named indexers. - Dispinterfaces can only be called through
IDispatch::Invoke, so they become late-bound wrapper classes with methods, properties and indexers that invoke the members by their DISPIDs. - Coclasses become static classes with the
Clsidand aCreate()method that returns the default interface;Create(unique: true)returns it in an object of its own, which can be released at once, see Interface pointers. With--dynamic, coclasses whose default interface supportsIDispatchalso getCreateDynamic(), see Late binding withdynamic. Their event ([source]) interfaces get a handler interface and a sink class, see Events. - Enums become enums and structs and unions become structs with the layout of the COM structures, whose
DATE,CURRENCYandVARIANT_BOOLmembers are converted by properties, see Structures. - Modules become static classes with
[LibraryImport]functions and constants. - Aliases are replaced by the aliased types.
- Types imported from other type libraries are generated into the namespace of their library,
Interop.<Library>by default, see Several sources. The well-known OLE Automation typesIUnknown,IDispatch,IEnumVARIANTandGUIDare mapped toobject,IDispatch,IEnumVARIANTandGuid.
| Type library | C# |
|---|---|
BSTR |
string? (configurable, see BSTRs) |
LPSTR, LPWSTR |
string? with MarshalAs |
VARIANT_BOOL |
bool (configurable, see below) |
VARIANT |
object? (VariantMarshaller; null is an omitted argument for optional parameters) |
IUnknown* |
object? |
IDispatch* |
IDispatch? |
DECIMAL |
decimal |
CURRENCY |
decimal (CurrencyMarshaller; configurable, see below) |
DATE |
DateTime (DateMarshaller; configurable, see below) |
SAFEARRAY(T) |
T[]? (SafeArrayMarshaller<T>; configurable, see SAFEARRAYs) |
[out] T*, [in, out] T* |
out T, ref T |