C# / .NET · recipe

Decode custom structures in C#

Load the server's data type system, then read the structured value and access it through its decoded form. Without the type system loaded you get an ExtensionObject holding bytes you cannot interpret.

StackOPCFoundation/UA-.NETStandard
Version1.5.378.156 stable, 2.0.0-preview.2

The code

using Opc.Ua.Client.ComplexTypes;

// build CLR types from the server's DataTypeDefinitions at runtime
var typeSystem = new ComplexTypeSystem(session);
await typeSystem.Load();

var nodeId = new NodeId(7001, (ushort)nsIndex);
DataValue dv = session.ReadValue(nodeId);

if (dv.Value is ExtensionObject eo && eo.Body is BaseComplexType complex)
{
    Console.WriteLine($"decoded as {complex.TypeId}");
    foreach (var prop in complex.GetPropertyEnumerator())
        Console.WriteLine($"  {prop.Name} = {complex[prop.Name]}");
}
else
{
    Console.WriteLine("still an opaque ExtensionObject - type system not loaded?");
}

What it prints

decoded as ns=4;i=3001
  Identifier = JOB-2026-0912
  State = 2
  PartsPlanned = 500
  PartsProduced = 431

Notes

  • ComplexTypeSystem.Load() reads the server's DataTypeDefinition attributes and emits CLR types dynamically. Call it once after the session is up.
  • If types still will not decode, the server may only publish a legacy binary type dictionary. That is a genuine gap between the two mechanisms, not a client defect.
  • Unions and optional fields break first. Test those specifically.
  • The reference stack can also generate types at build time from a NodeSet2 file, which is preferable when the model is stable and known.

What breaks first

The structure reads as an ExtensionObject with an unreadable body BadDataEncodingUnsupported
Load the complex type system after connecting. If the server offers only a type dictionary, use a client that reads dictionaries or change the server.
Other recipes in this stack

Last verified 7 September 2026 against OPC UA 1.05.07. OPC UA Connect is an educational resource and is not affiliated with the OPC Foundation.

Free download
OPC UA Troubleshooting & Integration Checklist
The field-tested checklist engineers use to diagnose certificates, ports and namespace problems fast, plus a Python (asyncua) client template.
Get the free checklist →
Free, no course, no login