ASN.1/C# Generated Code

Applies to: ASN.1/C# 4.5

The OSS ASN.1 compiler for C# generates the following structures:


On disk, a separate subdirectory is created for each module namespace, and each class is placed in a separate file. The compiler also generates two additional files:

Type mapping file
A text file that lists all the named types in the ASN.1 specification along with the C# types to which they are mapped. It can be used by the application programmer to determine how certain ASN.1 types are represented.
Response File
Lists all the generated files.
The <MySchema> name of the root folder is derived from the .asn file name (the last one, if multiple) and can be changed by the compiler option -output <name>.



NOTE: When the –singleFile compiler option is used, the generated code is placed in a single file.


By default, the generated namespace is derived from the ASN.1 file name and the ASN.1 module name.



When the –singleNs compiler option is used, all the generated code is placed in the project namespace.

Classes that are related to the entire specification and are not tied to a particular ASN.1 module (encoder/decoder classes), are placed in the top level (schema) namespace.

NOTE: For more information, see the following compiler options that override the default namespace: -output, -names and -namespace.

Type Representation

The OSS ASN.1 compiler generates C# code that represents the types defined in the ASN.1 schema files.

ASN.1 types that are PDUs (unreferenced by other types, contained in an open type, or explicitly marked with the PDU directive) are always represented by a compiler-generated class inherited from the Oss.Asn1.BasePdu runtime class. Non-PDU ASN.1 types are represented as generated classes inherited from the Oss.Asn1.BaseType runtime library class. These classes will implement the internal encode and decode methods only when they are multi-referenced.

Primitive ASN.1 types are represented by the native .NET runtime classes (for example, BOOLEAN is represented by bool, UTF8String by string, INTEGER by int or by long, if the range constraint requires 64-bit and so on).

NOTE: When you define a new type in the ASN.1 schema, based on a primitive type, for example, Counter ::= INTEGER, the type is also represented by an int (unless Counter is a PDU, which is unlikely). If your application will reference this type as Counter, you can add an alias via the C# directive:

using Counter = System.Int32;

ASN.1 arrays, such as SEQUENCE OF or SET OF, are represented by a class wrapping the .NET List<> object.

ASN.1 date/time types are represented as C# ISO 8601 formatted strings, which can be converted from/to native .NET Date/Time classes (see the MSDN .NET Standard Date and Time Format Strings, Round-trip date/time pattern).

ASN.1 specialized types, such as OBJECT IDENTIFIER or BIT STRING, are represented by classes with an intuitive interface that can manipulate all aspects of the ASN.1 type. For example, Oss.Asn1.BitString exposes the Set/Get/And/Or/Xor etc., bitwise method.

ASN.1 information objects are represented by a class containing individual fields of the Information Object, just like any other constructed type, such as SEQUENCE. Representation of ASN.1 information object sets depends on the presence of the UNIQUE field:

  • Without the UNIQUE field, it is a simple list: System.Collections.Generic.List< MyInfoObj >
  • With the UNIQUE field, it is a dictionary that internally employs System.Collections.Generic.Dictionary< MyUniqueField, MyInfoObj >: Oss.Asn1.FastObjectSet< MyUniqueField, MyInfoObj >

Names and Identifiers

The names of the compiler-generated C# constructs (classes, members) are derived from the names of the corresponding ASN.1 constructs. Dashes ("-") are removed, and the first letter of the identifier and of each subsequent concatenated word are capitalized. The compiler applies a disambiguation algorithm to ensure that all names are unique within the current scope of reference and do not conflict with C# reserved keywords.

NOTE: For more information about how to change the default handling of dashes and capitalization, see the –names compiler option.

To represent "anonymous" types, such as those specified inline or inside a CHOICE or a SEQUENCE, the name of the field is used with the suffix "Type" added.


ASN.1 C#
Apples ::= SEQUENCE
   . . .
   color ENUMERATED {green, yellow, red}
public class Apples : Oss.Asn1.BasePdu
  . . .
  public enum ColorType
        {Green = 0, Yellow = 1, Red = 2}
  public ColorType Color { get; set; }
  . . .

Names for identifiers that are missing, such as field names inside a CHOICE or SEQUENCE and/or "anonymous" types, are derived from the ASN.1 type itself, with a number added when necessary.


ASN.1 C#
Oranges ::= SEQUENCE
  . . .
  ENUMERATED {small, medium, large}
public class Oranges: Oss.Asn1.BasePdu
  . . .
  public enum EnumeratedType
        {Small = 0, Medium = 1, Large = 2}
  public EnumeratedType Enumerated { get; set;}
  . . .


When values are included in the input ASN.1 file, they are also declared and initialized in the generated C# code. Note that both the ASN.1 and XML syntaxes are supported for values.


The following lines are equivalent:

studentAge INTEGER ::= 23
studentAge ::= <INTEGER>23</INTEGER>

This documentation applies to the OSS® ASN.1 Tools for C# release 4.5 and later.

Copyright © 2017 OSS Nokalva, Inc. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means electronic, mechanical, photocopying, recording or otherwise, without the prior permission of OSS Nokalva, Inc.
Every distributed copy of the OSS® ASN.1 Tools for C# is associated with a specific license and related unique license number. That license determines, among other things, what functions of the OSS ASN.1 Tools for C# are available to you.