The ASN.1 INTEGER type is converted to a C# class that inherits the Asn1Integer run-time class. This base class encapsulates the following public member variable:
public long mValue;
This is where the integer value to be encoded is stored. It also contains the result of a decode operation. Since it is public, it can be accessed directly to get or set the value. The generated constructors can also be used to set the value.
The following shows the basic mapping from ASN.1 type to C# class definition:
ASN.1 Production: |
<name> ::= INTEGER |
XSD Types: |
<xsd:integer>, <xsd:byte>, <xsd:short>, <xsd:int>, <xsd:long>, <xsd:unsignedByte>, <xsd:unsignedShort>, <xsd:unsignedInt>, <xsd:unsignedLong>, <xsd:positiveInt>, <xsd:nonPositiveInt>, <xsd:negativeInt>, <xsd:nonNegativeInt> |
Generated C# class: |
public class <name> : Asn1Integer { public <name> () : base() { } public <name> (long value_) : base(value_) { } } |
This shows the class generated for a simple INTEGER assignment. If a tagged or constrained type is specified, specific encode and decode methods will be generated as well.
Large Integer Support
The maximum size for a C# long integer type is 64 bits. ASN.1 has no such limitation on integer sizes and some applications (security key values for example) demand larger sizes. In order to accommodate these types of applications, the ASN1C compiler allows an integer to be declared a “big integer” via a configuration file variable (the <isBigInteger/ > setting is used to do this – see the section describing the configuration file for full details). When the compiler detects this setting, it will declare the integer class to be derived from the Asn1BigInteger class instead of the Asn1Integer class. The Asn1BigInteger class encapsulates an object of the C# BigInteger class. This provides full support for working with integers of arbitrary lengths.
For example, the following INTEGER type might be declared in the ASN.1 source file:
SecurityKeyType ::= [APPLICATION 2] INTEGER
Then, in a configuration file used with the ASN.1 definition above, the following declaration can be made:
<production> <name>SecurityKeyType</name> <isBigInteger/> </production>
This will cause the compiler to generate the following class header:
class SecurityKeyType : Asn1BigInteger
The value field is populated by creating a C# BigInteger object and either passing it in through the constructor or using it to directly populate the public member variable named mValue declared in the base class.