C-Sharp Mapping for Modules

On this page:

Default Mapping

Slice modules map to C# namespaces with the same name as the Slice module. The mapping preserves the nesting of the Slice definitions. For example:

Slice
module M1
{
    // Definitions for M1 here...
    module M2
    {
        // Definitions for M2 here...
    }
}

// ...

module M1    // Reopen M1
{
    // More definitions for M1 here...
}

This definition maps to the corresponding C# definitions:

C#
namespace M1
{
    namespace M2
    {
        // ...
    }
    // ...
}

// ...

namespace M1    // Reopen M1
{
    // ...
}

If a Slice module is reopened, the corresponding C# namespace is reopened as well.

Adding an Enclosing Namespace

You can also specify an enclosing C# namespace with the cs:namespace:namespace metadata directive, for example:

Slice
["cs:namespace:NS"]
module M1
{
    // Definitions for M1 here...
}

maps to

C#
namespace NS.M1
{
    // ...
}

Metadata directives such as cs:namespace:namespace do not change the contract between clients and servers, in particular the type IDs for Slice types are unaffected.

Customizing the Namespace for Type ID Helpers

When you add cs:namespace:namespace to a modulethe Slice compiler generates small C# helper classes for all Slice classes and exceptions defined in this module. These helper classes allow the Ice run time to find the generated C# class or exception that correspond to a given type ID. These helper classes are generated by default in the Ice.TypeId namespace, and this default should be suitable for most applications.

You can nevertheless instruct the Slice compiler to generate these type ID helper classes in a namespace of your choosing with the file metadata directive cs:typeid-namespace:namespace. For example:

Slice
[["cs:typeid-namespace:NS.TypeId"]] // generate all type ID helper classes in the NS.TypeId namespace
                                    // and not in the Ice.TypeId namespace


["cs:namespace:NS"]
module M1
{
    // Definitions for M1 here...
}

When you change the namespace for these type ID helper classes, you need to tell the Ice run time where to look for these type ID helpers by setting typeIdNamespaces in your communicator's InitializationData parameter. 

typeIdNamespaces is an array of strings, and by default this array contains only "Ice.TypeId",  With the example above, you would need to set this array to { "NS.TypeId" }. If your application uses Slice modules with different settings for cs:typeid-namespace:namespaceyou can add multiple namespaces to typeIdNamespaces, and the Ice run time will check these namespaces in order when resolving a type ID.


See Also