/// <summary>
        /// Gets the display text for the specified attribute.
        /// </summary>
        /// <param name="session">The currently active session.</param>
        /// <param name="attributeId">The id of the attribute.</param>
        /// <param name="value">The value of the attribute.</param>
        /// <returns>The attribute formatted as a string.</returns>
        public static string GetAttributeDisplayText(Session session, uint attributeId, Variant value)
        {
            if (value == Variant.Null)
            {
                return(String.Empty);
            }

            switch (attributeId)
            {
            case Attributes.AccessLevel:
            case Attributes.UserAccessLevel:
            {
                byte?field = value.Value as byte?;

                if (field != null)
                {
                    return(GetAccessLevelDisplayText(field.Value));
                }

                break;
            }

            case Attributes.EventNotifier:
            {
                byte?field = value.Value as byte?;

                if (field != null)
                {
                    return(GetEventNotifierDisplayText(field.Value));
                }

                break;
            }

            case Attributes.DataType:
            {
                return(session.NodeCache.GetDisplayText(value.Value as NodeId));
            }

            case Attributes.ValueRank:
            {
                int?field = value.Value as int?;

                if (field != null)
                {
                    return(GetValueRankDisplayText(field.Value));
                }

                break;
            }

            case Attributes.NodeClass:
            {
                int?field = value.Value as int?;

                if (field != null)
                {
                    return(((NodeClass)field.Value).ToString());
                }

                break;
            }

            case Attributes.NodeId:
            {
                NodeId field = value.Value as NodeId;

                if (!NodeId.IsNull(field))
                {
                    return(field.ToString());
                }

                return("Null");
            }

            case Attributes.DataTypeDefinition:
            {
                ExtensionObject field = value.Value as ExtensionObject;
                if (field != null)
                {
                    return(field.ToString());
                }
                break;
            }
            }

            // check for byte strings.
            if (value.Value is byte[])
            {
                return(Utils.ToHexString(value.Value as byte[]));
            }

            // use default format.
            return(value.ToString());
        }
Exemple #2
0
 public static Entry For(ExtensionObject ext)
 {
     return(new StringEntry(ext == null ? null : ext.ToString()));
 }
 /// <summary>
 ///
 /// </summary>
 /// <param name="lvi"></param>
 /// <param name="dv"></param>
 /// <param name="session"></param>
 protected virtual void UpdateValueInListView(ListViewItem lvi, DataValue dv, Session session)
 {
     if (dv.Value != null)
     {
         ExtensionObject extension = dv.Value as ExtensionObject;
         Array           array     = dv.Value as Array;
         if (array != null)
         {
             lvi.SubItems[(int)SubItemIndexes.Value].Text = String.Format("{0}[{1}]", dv.Value.GetType().GetElementType().Name, array.Length);
             ExtensionObject extension1 = array.GetValue(0) as ExtensionObject;
             if (extension1 != null)
             {
                 // This returns "Opc.Ua.ArrayType" where arraytype is the correct array type name, should use this for the type column
                 lvi.SubItems[(int)SubItemIndexes.Value].Text = extension1.ToString();
                 //int i = 0;
                 foreach (object o in array)
                 {
                     ExtensionObject extension2 = o as ExtensionObject;
                     if (extension1 != null)
                     {
                         IEncodeable encodeable1 = extension2.Body as IEncodeable;
                         if (encodeable1 != null)
                         {
                             string name = encodeable1.GetType().Name;
                             //Utils.Trace("encodeable1.GetType().Name: {0}, array[{1}]", name, i++);
                             PropertyInfo[] properties = encodeable1.GetType().GetProperties();
                             foreach (PropertyInfo property in properties)
                             {
                                 //Utils.Trace("property.Name: {0},  property.GetValue: {1}", property.Name, property.GetValue(encodeable1, null) != null ? property.GetValue(encodeable1, null).ToString():"null");
                                 //Utils.Trace("property.Name: {0},  ", property.Name);
                             }
                         }
                     }
                 }
             }
             else
             {
                 StringBuilder sb = new StringBuilder();
                 int           i  = 1;
                 sb.Append("{");
                 foreach (object o in array)
                 {
                     sb.Append(o.ToString());
                     if (i++ < array.Length)
                     {
                         sb.Append(", ");
                     }
                 }
                 sb.Append("}");
                 lvi.SubItems[(int)SubItemIndexes.Value].Text = sb.ToString();
             }
         }
         else if (extension != null)
         {
             IEncodeable encodeable = extension.Body as IEncodeable;
             if (encodeable != null)
             {
                 lvi.SubItems[(int)SubItemIndexes.Value].Text = extension.ToString();;
             }
             else
             {
                 lvi.SubItems[(int)SubItemIndexes.Value].Text = extension.Body.ToString();
             }
         }
         else
         {
             lvi.SubItems[(int)SubItemIndexes.Value].Text = dv.Value.ToString();
         }
         lvi.ToolTipText = lvi.SubItems[(int)SubItemIndexes.Value].Text;
     }
 }
        public void ExtensionObject()
        {
            ExtensionObject extensionObject_null = null;
            // Validate the default constructor
            ExtensionObject extensionObject_Default = new Ua.ExtensionObject();

            Assert.NotNull(extensionObject_Default);
            Assert.AreEqual(ExpandedNodeId.Null, extensionObject_Default.TypeId);
            Assert.AreEqual(ExtensionObjectEncoding.None, extensionObject_Default.Encoding);
            Assert.Null(extensionObject_Default.Body);
            // Constructor by ExtensionObject
            ExtensionObject extensionObject = new ExtensionObject(ExpandedNodeId.Null);

            Assert.NotNull(extensionObject);
            Assert.AreEqual(ExpandedNodeId.Null, extensionObject.TypeId);
            Assert.AreEqual(ExtensionObjectEncoding.None, extensionObject.Encoding);
            Assert.Null(extensionObject.Body);
            // static extensions
            Assert.True(Ua.ExtensionObject.IsNull(extensionObject));
            Assert.Null(Ua.ExtensionObject.ToEncodeable(null));
            Assert.Null(Ua.ExtensionObject.ToArray(null, typeof(object)));
            Assert.Null(Ua.ExtensionObject.ToList <object>(null));
            // constructor by ExpandedNodeId
            extensionObject = new ExtensionObject((ExpandedNodeId)null);
            Assert.AreEqual(0, extensionObject.GetHashCode());
            Assert.Throws <ArgumentNullException>(() => new ExtensionObject(extensionObject_null));
            Assert.Throws <ServiceResultException>(() => new ExtensionObject(new object()));
            // constructor by object
            object byteArray = new byte[] { 1, 2, 3 };

            extensionObject = new ExtensionObject(byteArray);
            Assert.NotNull(extensionObject);
            Assert.AreEqual(extensionObject, extensionObject);
            // string extension
            var extensionObjectString = extensionObject.ToString();

            Assert.Throws <FormatException>(() => extensionObject.ToString("123", null));
            Assert.NotNull(extensionObjectString);
            // clone
            var clonedExtensionObject = (ExtensionObject)Utils.Clone(extensionObject);

            Assert.AreEqual(extensionObject, clonedExtensionObject);
            // IsEqual operator
            clonedExtensionObject.TypeId = new ExpandedNodeId(333);
            Assert.AreNotEqual(extensionObject, clonedExtensionObject);
            Assert.AreNotEqual(extensionObject, extensionObject_Default);
            Assert.AreNotEqual(extensionObject, new object());
            Assert.AreEqual(clonedExtensionObject, clonedExtensionObject);
            Assert.AreEqual(ExpandedNodeId.Null, extensionObject.TypeId);
            Assert.AreEqual(ExpandedNodeId.Null.GetHashCode(), extensionObject.TypeId.GetHashCode());
            Assert.AreEqual(ExtensionObjectEncoding.Binary, extensionObject.Encoding);
            Assert.AreEqual(byteArray, extensionObject.Body);
            Assert.AreEqual(byteArray.GetHashCode(), extensionObject.Body.GetHashCode());
            // collection
            ExtensionObjectCollection collection = new ExtensionObjectCollection();

            Assert.NotNull(collection);
            collection = new ExtensionObjectCollection(100);
            Assert.NotNull(collection);
            collection = new ExtensionObjectCollection(collection);
            Assert.NotNull(collection);
            collection = (ExtensionObjectCollection)collection.MemberwiseClone();
            // default value is null
            Assert.Null(TypeInfo.GetDefaultValue(BuiltInType.ExtensionObject));
        }