/// <summary>
 /// Add an enum type defined in a binary schema dictionary.
 /// </summary>
 private void AddEnumTypes(
     IComplexTypeBuilder complexTypeBuilder,
     IList <Opc.Ua.Schema.Binary.TypeDescription> enumList,
     IList <INode> enumerationTypes
     )
 {
     foreach (var item in enumList)
     {
         Type         newType  = null;
         DataTypeNode enumType = enumerationTypes.Where(node =>
                                                        node.BrowseName.Name == item.Name &&
                                                        (node.NodeId.NamespaceIndex == complexTypeBuilder.TargetNamespaceIndex ||
                                                         complexTypeBuilder.TargetNamespaceIndex == -1)).FirstOrDefault()
                                 as DataTypeNode;
         if (enumType != null)
         {
             // try dictionary enum definition
             var enumeratedObject = item as Schema.Binary.EnumeratedType;
             if (enumeratedObject != null)
             {
                 // 1. use Dictionary entry
                 newType = complexTypeBuilder.AddEnumType(enumeratedObject);
             }
             if (newType == null)
             {
                 var dataType = m_session.NodeCache.Find(enumType.NodeId) as DataTypeNode;
                 if (dataType != null)
                 {
                     if (dataType.DataTypeDefinition != null)
                     {
                         // 2. use DataTypeDefinition
                         newType = complexTypeBuilder.AddEnumType(enumType.BrowseName.Name, dataType.DataTypeDefinition);
                     }
                     else
                     {
                         // browse for EnumFields or EnumStrings property
                         var property  = BrowseForSingleProperty(enumType.NodeId);
                         var enumArray = m_session.ReadValue(
                             ExpandedNodeId.ToNodeId(property.NodeId, m_session.NamespaceUris));
                         if (enumArray.Value is ExtensionObject[])
                         {
                             // 3. use EnumValues
                             newType = complexTypeBuilder.AddEnumType(enumType.BrowseName.Name, (ExtensionObject[])enumArray.Value);
                         }
                         else if (enumArray.Value is LocalizedText[])
                         {
                             // 4. use EnumStrings
                             newType = complexTypeBuilder.AddEnumType(enumType.BrowseName.Name, (LocalizedText[])enumArray.Value);
                         }
                     }
                 }
             }
             if (newType != null)
             {
                 // match namespace and add to type factory
                 AddEncodeableType(enumType.NodeId, newType);
             }
         }
     }
 }
Exemple #2
0
        /// <summary>
        /// Add an enum type defined in a DataType node.
        /// </summary>
        private Type AddEnumType(
            IComplexTypeBuilder complexTypeBuilder,
            DataTypeNode enumTypeNode
            )
        {
            Type newType = null;

            if (enumTypeNode != null)
            {
                string name = enumTypeNode.BrowseName.Name;
                if (enumTypeNode.DataTypeDefinition != null)
                {
                    // 1. use DataTypeDefinition
                    newType = complexTypeBuilder.AddEnumType(name, enumTypeNode.DataTypeDefinition);
                }
                else
                {
                    // browse for EnumFields or EnumStrings property
                    var property = BrowseForSingleProperty(enumTypeNode.NodeId);
                    if (property != null)
                    {
                        var enumArray = m_session.ReadValue(
                            ExpandedNodeId.ToNodeId(property.NodeId,
                                                    m_session.NamespaceUris));
                        if (enumArray.Value is ExtensionObject[])
                        {
                            // 2. use EnumValues
                            newType = complexTypeBuilder.AddEnumType(name, (ExtensionObject[])enumArray.Value);
                        }
                        else if (enumArray.Value is LocalizedText[])
                        {
                            // 3. use EnumStrings
                            newType = complexTypeBuilder.AddEnumType(name, (LocalizedText[])enumArray.Value);
                        }
                    }
                }
            }
            return(newType);
        }