Opc.Ua.BinaryDecoder.ReadExtensionObject C# (CSharp) Method

ReadExtensionObject() private method

Reads an extension object from the stream.
private ReadExtensionObject ( ) : Opc.Ua.ExtensionObject
return Opc.Ua.ExtensionObject
        private ExtensionObject ReadExtensionObject()
        {
            ExtensionObject extension = new ExtensionObject();

            // read type id.
            NodeId typeId = ReadNodeId(null);
            
            // convert to absolute node id.
            extension.TypeId = NodeId.ToExpandedNodeId(typeId, m_context.NamespaceUris);
            
            if (!NodeId.IsNull(typeId) && NodeId.IsNull(extension.TypeId))
            {             
                Utils.Trace(
                    "Cannot de-serialized extension objects if the NamespaceUri is not in the NamespaceTable: Type = {0}", 
                    typeId);
            }

            // read encoding.
            ExtensionObjectEncoding encoding = (ExtensionObjectEncoding)Enum.ToObject(typeof(ExtensionObjectEncoding), m_reader.ReadByte());

            // nothing more to do for empty bodies.
            if (encoding == ExtensionObjectEncoding.None)
            {
                return extension;
            }

            // check for known type.
            Type systemType = m_context.Factory.GetSystemType(extension.TypeId);

            // check for XML bodies.
            if (encoding == ExtensionObjectEncoding.Xml)
            {
                extension.Body = ReadXmlElement(null);

                // attempt to decode a known type.
                if (systemType != null)
                {
                    XmlElement element = extension.Body as XmlElement;
                    XmlDecoder xmlDecoder = new XmlDecoder(element, this.Context);

                    try
                    {
                        xmlDecoder.PushNamespace(element.NamespaceURI);
                        IEncodeable body = xmlDecoder.ReadEncodeable(element.LocalName, systemType);
                        xmlDecoder.PopNamespace();

                        // update body.
                        extension.Body = body;
                    }
                    catch (Exception e)
                    {
                        Utils.Trace("Could not decode known type {0}. Error={1}, Value={2}", systemType.FullName, e.Message, element.OuterXml);
                    }
                }

                return extension;
            }

            // create instance of type.
            IEncodeable encodeable = null;

            if (systemType != null)
            {
                encodeable = Activator.CreateInstance(systemType) as IEncodeable;
            }

            // get the length.
            int length = ReadInt32(null);

            // process unknown type.
            if (encodeable == null)
            {
                // figure out how long the object is.
                if (length == -1)
                {
                    throw new ServiceResultException(
                        StatusCodes.BadDecodingError,
                        Utils.Format("Cannot determine length of unknown extension object body with type '{0}'.", extension.TypeId));
                }
                
                // check the length.
                if (m_context.MaxByteStringLength > 0 && m_context.MaxByteStringLength < length)
                {
                    throw ServiceResultException.Create(
                        StatusCodes.BadEncodingLimitsExceeded, 
                        "MaxByteStringLength {0} < {1}", 
                        m_context.MaxByteStringLength,
                        length);
                }

                // read the bytes of the body.
                extension.Body = m_reader.ReadBytes(length);
                return extension;
            }

            // save the current position.
            int start = Position;
            
            // decode body.            
            encodeable.Decode(this);

            // skip any unread data.
            int unused = length - (Position - start);

            if (unused > 0)
            {
                m_reader.ReadBytes(unused);
            }
                        
            extension.Body = encodeable;
            return extension;
        }
        #endregion

Same methods

BinaryDecoder::ReadExtensionObject ( string fieldName ) : Opc.Ua.ExtensionObject

Usage Example

示例#1
0
        /// <summary>
        /// Converts a VARIANT value to a Builtin Type.
        /// </summary>
        private object VariantValueToScalarValue(object value, NodeId builtinTypeId)
        {        
            switch ((uint)builtinTypeId.Identifier)
            {
                case DataTypes.Guid:
                {
                    return new Uuid((string)value);
                }

                case DataTypes.XmlElement:
                {    
                    XmlDocument document = new XmlDocument();
                    document.InnerXml = (string)value;
                    return document.DocumentElement;
                }

                case DataTypes.NodeId:
                {
                    return NodeId.Parse((string)value);
                }

                case DataTypes.ExpandedNodeId:
                {
                    return ExpandedNodeId.Parse((string)value);
                }

                case DataTypes.QualifiedName:
                {
                    return QualifiedName.Parse((string)value);
                }

                case DataTypes.LocalizedText:
                {
                    return new LocalizedText(ComUtils.GetLocale(m_lcid), (string)value);
                }

                case DataTypes.StatusCode:
                {
                     return new StatusCode((uint)value);
                }

                case DataTypes.DiagnosticInfo:
                {
                    BinaryDecoder decoder = new BinaryDecoder((byte[])value, m_session.MessageContext);
                    DiagnosticInfo decodedValue = decoder.ReadDiagnosticInfo(null);
                    decoder.Close();
                    return decodedValue; 
                }

                case DataTypes.DataValue:
                {
                    BinaryDecoder decoder = new BinaryDecoder((byte[])value, m_session.MessageContext);
                    DataValue decodedValue = decoder.ReadDataValue(null);
                    decoder.Close();
                    return decodedValue; 
                }

                case DataTypes.Structure:
                {
                    BinaryDecoder decoder = new BinaryDecoder((byte[])value, m_session.MessageContext);
                    ExtensionObject decodedValue = decoder.ReadExtensionObject(null);
                    decoder.Close();
                    return decodedValue; 
                }
            }

            return value;
        }