CHOICE

The ASN.1 CHOICE type is converted to a C# class that inherits the Asn1Choice run-time base class. This base class contains protected member variables to hold the choice element object and a selector value to specify which item in the CHOICE was chosen. Methods are generated to get and set the base class members.

The following shows the basic mapping from ASN.1 type to C# class definition:

ASN.1 Production:
<name> ::= CHOICE {
   <element1-name> <element1-type>,
   <element2-name> <element2-type>,
   ...
}
XSD Types:
<xsd:choice>, <xsd:union>
Generated C# class:
   public class <name> : Asn1Choice {
  public const byte _<ELEMENT1-NAME> 1
  public const byte _<ELEMENT2-NAME> 2
  ...

  public <name> () : base() { }

  public override string ElemName { … }

  public void Set_<element1-name> () { … }
  public void Set_<element2-name> () { … }
  ...
  public override void Decode () { … }
  public override int Encode () { … }
  public override void Print () { … }
}

Notes:

  1. The … notation used in the ASN.1 definition above does not represent the ASN.1 extensibility notation. It is used to show a continuation of the sequence elements.

  2. The public and private methods that are generated are shown without arguments or function bodies for brevity.

The compiler generates sequential identification constants for each of the defined elements in the CHOICE construct. The format used is the element names converted to all uppercase characters and preceded by an underscore. The constants represent the values returned by the base class ChoiceID property can therefore be used to determine what type of choice element was received in a decode operation.

The ElemName property is generated by the compiler and returns the name of the selected element.

A series of Set_<element> methods are generated for setting the element value. In these declarations, <element> would be replaced with the actual element names. This is the only way an element value can be set for encoding; these methods ensure a consistent setting of both the element identifier and object reference values.

To access the value of a generated CHOICE object, the ChoiceID property and GetElement methods within the base class are used. This is generally done with an if or switch statement as follows:

   Asn1BMPString element;
      if (aliasAddress.ChoiceID == AliasAddress._H323_ID) {
   element = (Asn1BMPString) aliasAddress.GetElement();
   }

In this case, ChoiceID is invoked and the result tested to see if the expected value was received. If it was, the element is assigned using GetElement with a cast operation.