Opc.Ua.NumericRange.ApplyRange C# (CSharp) Method

ApplyRange() private method

private ApplyRange ( object &value ) : Opc.Ua.StatusCode
value object
return Opc.Ua.StatusCode
        public StatusCode ApplyRange(ref object value)
        {   
            // check for empty range.
            if (this.m_begin == -1 && this.m_end == -1)
            {
                return StatusCodes.Good;
            }

            // nothing to do for null values.
            if (value == null)
            {
                return StatusCodes.Good;
            }
            
            Array array = value as Array;
            
            // check for list type.
            IList list = null;     
            TypeInfo typeInfo = null;
            
            if (array == null)
            {
                list = value as IList;

                if (list != null)
                {
                    typeInfo = TypeInfo.Construct(list);
                }
            }

            bool isString = false;

            // check for array.
            if (array == null && list == null)
            {             
                // check for string.
                String chars = value as String;
                
                if (chars == null)
                {
                    value = null;
                    return StatusCodes.BadIndexRangeNoData;
                }
                
                isString = true;
                array = chars.ToCharArray();
            }

            // check for multidimensional arrays.
            if (m_subranges != null)
            {
                return ApplyMultiRange(ref value);               
            }

            // get length.
            int length = 0;

            if (list != null)
            {
                length = list.Count;
            }
            else
            {
                length = array.Length;
            }

            int begin = this.m_begin;

            // choose a default start.
            if (begin == -1)
            {
                begin = 0;
            }

            // return an empty array if begin is beyond the end of the array.
            if (begin >= length)
            {
                value = null;
                return StatusCodes.BadIndexRangeNoData;
            }
   
            // only copy if actually asking for a subset.
            int end = this.m_end;
            
            // check if looking for a single element.
            if (end == -1)
            {
                end = begin;
            }

            // ensure end of array is not exceeded.
            else if (end >= length-1)
            {
                end = length-1;
            }

            Array clone = null;
            int subLength = end-begin+1;
            
            // check for list.
            if (list != null && typeInfo != null)
            {
                clone = TypeInfo.CreateArray(typeInfo.BuiltInType, subLength);

                for (int ii = begin; ii < subLength; ii++)
                {
                    clone.SetValue(list[ii], ii-begin);
                }

                return StatusCodes.Good;
            }

            // handle array or string.
            if (isString)
            {            
                clone = new char[subLength];
            }
            else
            {
                clone = Array.CreateInstance(array.GetType().GetElementType(), subLength);
            }

            Array.Copy(array, begin, clone, 0, clone.Length);
            
            if (isString)
            {
                value = new string((char[])clone);
            }
            else
            {
                value = clone;
            }

            return StatusCodes.Good;
        }

Usage Example

コード例 #1
0
        /// <summary>
        /// Returns the value of the attribute for the specified child.
        /// </summary>
        /// <param name="context">The context to use when evaluating the operand.</param>
        /// <param name="typeDefinitionId">The type of the instance.</param>
        /// <param name="relativePath">The path from the instance to the node which defines the attribute.</param>
        /// <param name="attributeId">The attribute to return.</param>
        /// <param name="indexRange">The sub-set of an array value to return.</param>
        /// <returns>
        /// The attribute value. Returns null if the attribute does not exist.
        /// </returns>
        public object GetAttributeValue(
            FilterContext context,
            NodeId typeDefinitionId,
            IList <QualifiedName> relativePath,
            uint attributeId,
            NumericRange indexRange)
        {
            if (!NodeId.IsNull(typeDefinitionId))
            {
                if (!context.TypeTree.IsTypeOf(m_typeDefinitionId, typeDefinitionId))
                {
                    return(null);
                }
            }

            object value = GetAttributeValue(
                m_snapshot,
                relativePath,
                0,
                attributeId);

            if (indexRange != NumericRange.Empty)
            {
                StatusCode error = indexRange.ApplyRange(ref value);

                if (StatusCode.IsBad(error))
                {
                    value = null;
                }
            }

            return(value);
        }
All Usage Examples Of Opc.Ua.NumericRange::ApplyRange