Opc.Ua.XmlEncoder.WriteVariantContents C# (CSharp) Method

WriteVariantContents() private method

private WriteVariantContents ( object value, TypeInfo typeInfo ) : void
value object
typeInfo System.Reflection.TypeInfo
return void
        public void WriteVariantContents(object value, TypeInfo typeInfo)
        {
            // check for null.
            if (value == null)
            {
                m_writer.WriteStartElement("Null", Namespaces.OpcUaXsd);
                m_writer.WriteEndElement();
                return;
            }

            try
            {
                PushNamespace(Namespaces.OpcUaXsd);

                // write scalar.
                if (typeInfo.ValueRank < 0)
                {
                    switch (typeInfo.BuiltInType)
                    {
                        case BuiltInType.Boolean: { WriteBoolean("Boolean", (bool)value); return; }
                        case BuiltInType.SByte: { WriteSByte("SByte", (sbyte)value); return; }
                        case BuiltInType.Byte: { WriteByte("Byte", (byte)value); return; }
                        case BuiltInType.Int16: { WriteInt16("Int16", (short)value); return; }
                        case BuiltInType.UInt16: { WriteUInt16("UInt16", (ushort)value); return; }
                        case BuiltInType.Int32: { WriteInt32("Int32", (int)value); return; }
                        case BuiltInType.UInt32: { WriteUInt32("UInt32", (uint)value); return; }
                        case BuiltInType.Int64: { WriteInt64("Int64", (long)value); return; }
                        case BuiltInType.UInt64: { WriteUInt64("UInt64", (ulong)value); return; }
                        case BuiltInType.Float: { WriteFloat("Float", (float)value); return; }
                        case BuiltInType.Double: { WriteDouble("Double", (double)value); return; }
                        case BuiltInType.String: { WriteString("String", (string)value); return; }
                        case BuiltInType.DateTime: { WriteDateTime("DateTime", (DateTime)value); return; }
                        case BuiltInType.Guid: { WriteGuid("Guid", (Uuid)value); return; }
                        case BuiltInType.ByteString: { WriteByteString("ByteString", (byte[])value); return; }
                        case BuiltInType.XmlElement: { WriteXmlElement("XmlElement", (XmlElement)value); return; }
                        case BuiltInType.NodeId: { WriteNodeId("NodeId", (NodeId)value); return; }
                        case BuiltInType.ExpandedNodeId: { WriteExpandedNodeId("ExpandedNodeId", (ExpandedNodeId)value); return; }
                        case BuiltInType.StatusCode: { WriteStatusCode("StatusCode", (StatusCode)value); return; }
                        case BuiltInType.QualifiedName: { WriteQualifiedName("QualifiedName", (QualifiedName)value); return; }
                        case BuiltInType.LocalizedText: { WriteLocalizedText("LocalizedText", (LocalizedText)value); return; }
                        case BuiltInType.ExtensionObject: { WriteExtensionObject("ExtensionObject", (ExtensionObject)value); return; }
                        case BuiltInType.DataValue: { WriteDataValue("DataValue", (DataValue)value); return; }
                        case BuiltInType.Enumeration: { WriteInt32("Int32", (int)value); return; }
                    }
                }
                
                // write array.
                else if (typeInfo.ValueRank <= 1)
                {
                    switch (typeInfo.BuiltInType)
                    {
                        case BuiltInType.Boolean: { WriteBooleanArray("ListOfBoolean", (bool[])value); return; }
                        case BuiltInType.SByte: { WriteSByteArray("ListOfSByte", (sbyte[])value); return; }
                        case BuiltInType.Byte: { WriteByteArray("ListOfByte", (byte[])value); return; }
                        case BuiltInType.Int16: { WriteInt16Array("ListOfInt16", (short[])value); return; }
                        case BuiltInType.UInt16: { WriteUInt16Array("ListOfUInt16", (ushort[])value); return; }
                        case BuiltInType.Int32: { WriteInt32Array("ListOfInt32", (int[])value); return; }
                        case BuiltInType.UInt32: { WriteUInt32Array("ListOfUInt32", (uint[])value); return; }
                        case BuiltInType.Int64: { WriteInt64Array("ListOfInt64", (long[])value); return; }
                        case BuiltInType.UInt64: { WriteUInt64Array("ListOfUInt64", (ulong[])value); return; }
                        case BuiltInType.Float: { WriteFloatArray("ListOfFloat", (float[])value); return; }
                        case BuiltInType.Double: { WriteDoubleArray("ListOfDouble", (double[])value); return; }
                        case BuiltInType.String: { WriteStringArray("ListOfString", (string[])value); return; }
                        case BuiltInType.DateTime: { WriteDateTimeArray("ListOfDateTime", (DateTime[])value); return; }
                        case BuiltInType.Guid: { WriteGuidArray("ListOfGuid", (Uuid[])value); return; }
                        case BuiltInType.ByteString: { WriteByteStringArray("ListOfByteString", (byte[][])value); return; }
                        case BuiltInType.XmlElement: { WriteXmlElementArray("ListOfXmlElement", (XmlElement[])value); return; }
                        case BuiltInType.NodeId: { WriteNodeIdArray("ListOfNodeId", (NodeId[])value); return; }
                        case BuiltInType.ExpandedNodeId: { WriteExpandedNodeIdArray("ListOfExpandedNodeId", (ExpandedNodeId[])value); return; }
                        case BuiltInType.StatusCode: { WriteStatusCodeArray("ListOfStatusCode", (StatusCode[])value); return; }
                        case BuiltInType.QualifiedName: { WriteQualifiedNameArray("ListOfQualifiedName", (QualifiedName[])value); return; }
                        case BuiltInType.LocalizedText: { WriteLocalizedTextArray("ListOfLocalizedText", (LocalizedText[])value); return; }
                        case BuiltInType.ExtensionObject: { WriteExtensionObjectArray("ListOfExtensionObject", (ExtensionObject[])value); return; }
                        case BuiltInType.DataValue: { WriteDataValueArray("ListOfDataValue", (DataValue[])value); return; }
                        
                        case BuiltInType.Enumeration: 
                        {
                            Enum[] enums = value as Enum[];
                            int[] ints = new int[enums.Length];

                            for (int ii = 0; ii < enums.Length; ii++)
                            {
                                ints[ii] = (int)(object)enums[ii];
                            }

                            WriteInt32Array("ListOfInt32", ints);
                            return; 
                        }

                        case BuiltInType.Variant:
                        {
                            Variant[] variants = value as Variant[];

                            if (variants != null)
                            {
                                WriteVariantArray("ListOfVariant", variants);
                                return;
                            }

                            object[] objects = value as object[];

                            if (objects != null)
                            {
                                WriteObjectArray("ListOfVariant", objects);
                                return;
                            }

                            throw ServiceResultException.Create(
                                StatusCodes.BadEncodingError,
                                "Unexpected type encountered while encoding an array of Variants: {0}",
                                value.GetType());
                        }
                    }
                }

                // write matrix.
                else if (typeInfo.ValueRank > 1)
                {
                    WriteMatrix("Matrix", (Matrix)value);
                    return;
                }

                // oops - should never happen.
                throw new ServiceResultException(
                    StatusCodes.BadEncodingError,
                    Utils.Format("Type '{0}' is not allowed in an Variant.", value.GetType().FullName));
            }
            finally
            {
                PopNamespace();
            }
        }
        

Usage Example

コード例 #1
0
        /// <summary>
        /// Sets the value shown in the control.
        /// </summary>
        private void SetValue(Variant value)
        {
            ValueTB.ForeColor = Color.Empty;
            ValueTB.Font = new Font(ValueTB.Font, FontStyle.Regular);

            m_textChanged = false;

            // check for null.
            if (Variant.Null == value)
            {
                ValueTB.Text = String.Empty;
                m_value = Variant.Null;
                return;
            }

            // get the source type.
            TypeInfo sourceType = value.TypeInfo;

            if (sourceType == null)
            {
                sourceType = TypeInfo.Construct(value.Value);
            }

            m_value = new Variant(value.Value, sourceType);
            
            // display value as text.
            StringBuilder buffer = new StringBuilder();
            XmlWriter writer = XmlWriter.Create(buffer, new XmlWriterSettings() { Indent = true, OmitXmlDeclaration = true });
            XmlEncoder encoder = new XmlEncoder(new XmlQualifiedName("Value", Namespaces.OpcUaXsd), writer, m_session.MessageContext);
            encoder.WriteVariantContents(m_value.Value, m_value.TypeInfo);
            writer.Close();

            ValueTB.Text = buffer.ToString();

            // extract the encoding id from the value.
            ExpandedNodeId encodingId = null;
            ExtensionObjectEncoding encoding = ExtensionObjectEncoding.None;

            if (sourceType.BuiltInType == BuiltInType.ExtensionObject)
            {
                ExtensionObject extension = null;

                if (sourceType.ValueRank == ValueRanks.Scalar)
                {
                    extension = (ExtensionObject)m_value.Value;
                }
                else
                {
                    // only use the first item in the list for arrays.
                    ExtensionObject[] list = (ExtensionObject[])m_value.Value;

                    if (list.Length > 0)
                    {
                        extension = list[0];
                    }
                }

                encodingId = extension.TypeId;
                encoding = extension.Encoding;
            }

            if (encodingId == null)
            {
                StatusCTRL.Visible = false;
                return;
            }

            // check if the encoding is known.
            IObject encodingNode = m_session.NodeCache.Find(encodingId) as IObject;

            if (encodingNode == null)
            {
                StatusCTRL.Visible = false;
                return;
            }

            // update the encoding shown.
            if (encoding == ExtensionObjectEncoding.EncodeableObject)
            {
                EncodingCB.Text = "(Converted to XML by Client)";
            }
            else
            {
                EncodingCB.Text = m_session.NodeCache.GetDisplayText(encodingNode);
            }

            m_encodingName = encodingNode.BrowseName;

            // find the data type for the encoding.
            IDataType dataTypeNode = null;

            foreach (INode node in m_session.NodeCache.Find(encodingNode.NodeId, Opc.Ua.ReferenceTypeIds.HasEncoding, true, false))
            {
                dataTypeNode = node as IDataType;

                if (dataTypeNode != null)
                {
                    break;
                }
            }

            if (dataTypeNode == null)
            {
                StatusCTRL.Visible = false;
                return;
            }

            // update data type display.
            DataTypeTB.Text = m_session.NodeCache.GetDisplayText(dataTypeNode);
            DataTypeTB.Tag = dataTypeNode;

            // update encoding drop down.
            EncodingCB.DropDownItems.Clear();

            foreach (INode node in m_session.NodeCache.Find(dataTypeNode.NodeId, Opc.Ua.ReferenceTypeIds.HasEncoding, false, false))
            {
                IObject encodingNode2 = node as IObject;

                if (encodingNode2 != null)
                {
                    ToolStripMenuItem item = new ToolStripMenuItem(m_session.NodeCache.GetDisplayText(encodingNode2));
                    item.Tag = encodingNode2;
                    item.Click += new EventHandler(EncodingCB_Item_Click);
                    EncodingCB.DropDownItems.Add(item);
                }
            }

            StatusCTRL.Visible = true;
        }