Accessing Schema Information

Starting with ASN.1/C# Tools version 4, you can use .NET Reflection and Attributes to access field names as they appear in the schema and see which field is optional, in other words, you can explore certain schema information. To do so, compile the schema with the -genSchemaInfo option to include information in the generated classes as .NET serialization attributes. For ASN.1 Studio, go to Project Settings > ASN.1 Compiler > Generated files > C# classes, and then select "Generate C# serialization attributes to capture type information from the ASN.1".



Top-Level-Msg ::= SEQUENCE { 
    nested Inner-Msg, 
    int INTEGER DEFAULT 255 }
Inner-Msg ::= SEQUENCE {
    string        UTF8String,
    optional-flag BOOLEAN OPTIONAL }

Here is the sample code that shows how to explore the schema:

using System.Runtime.Serialization; // Add a reference as well
using System.Linq;
// Use .NET reflection to access schema information

// (Generated with -genSchemaInfo)

// For more information, see the generated classes and the .NET DataMember attributes

Proj.MyModule.InnerMsg myData = new Proj.MyModule.InnerMsg();
myData.String = "Hello Attributes";

myData.OptionalFlag = true;

// Explore schema information (field names and optionality)
PropertyInfo[] props = myData.GetType()

.GetProperties(BindingFlags.Public | BindingFlags.Instance)

.Where( p => System.Attribute.IsDefined(p, 
typeof(DataMemberAttribute), false))
foreach (var prop in props)

foreach (DataMemberAttribute attr in prop.GetCustomAttributes(true))
System.Console.WriteLine("'{0}' is {1} required", 

attr.Name, attr.IsRequired ? "":"not");
// Output:
// 'string' is required

// 'optional-flag' is not required

NOTE 1: Starting with OSS ASN.1 C# Tools version 4, you can replace underscores in class names with dashes using the -names compiler option (Inner-Msg). In version 3, class names are separated by underscores by default (Inner_Msg).

NOTE 2: .NET attributes are not supported for version 3. To explore type information, a special SS.Asn1rt.Type.Asn1TypeExplorer class is required; however, if a particular field is optional, it is not possible to explore type information:

// Explore schema information (v3) 
OSS.Asn1rt.Type.Asn1TypeExplorer exp = 
new OSS.Asn1rt.Type.Asn1TypeExplorer(myData, true);

System.Console.WriteLine("Name '{0}'", exp.GetNodeTypeName());
} while (exp.GotoNextSibling());

Download files for this article. Note that you must first compile the .asn file to use the generated files with the Program.cs file.


C# | Reflection | Schema | .NET Attributes | Migration

The samples included with some of the Knowledge Center answers are meant for your general understanding of the OSS products. Different versions of the products might produce slightly different outputs. Consult the products documentation and samples for the most up-to-date products information and code examples.

Contact Support
contact Our office hours
24 hours/day, 7 days/week

  • Phone: 1-888-OSS-2761 (USA and Canada)
  • Phone: 1-732-302-9669 (International)
  • Fax: 1-732-302-0023
  • Email:
Free Trial

Test drive the OSS Nokalva ASN.1, 4G and XML Tools now! Your trial includes complete software, documentation, sample programs, free 24x7 technical support and more.

Learn ASN.1
Learn ASN.1

Our expert personnel can help you learn ASN.1!

We offer 4-day ASN.1 courses at our headquarters or your premises.