Exemple #1
0
        /// <summary>
        /// Get value from pin instance
        /// </summary>
        /// <typeparam name="T">Value type</typeparam>
        /// <param name="inPin">Pin instance</param>
        /// <returns>Value of type <see cref="T"/></returns>
        public T GetValue <T>(DataPin inPin)
        {
            if (inPin == null)
            {
                return(default(T));
            }

            var pinKey = BuildPinHash(inPin.TemporaryNodeId, inPin.Id);

            var value = default(T);

            if (!PinValues.Any(x => x.Key == pinKey) && Parent != null)
            {
                return(Parent.GetValue <T>(inPin));
            }
            else if (PinValues.Any(x => x.Key == pinKey))
            {
                var rawValue = PinValues.FirstOrDefault(x => x.Key == pinKey);

                try
                {
                    value = (T)rawValue.Value;
                }
                catch (InvalidCastException)
                {
                    if (rawValue.Value != null)
                    {
                        value = (T)TypeDescriptor.GetConverter(typeof(T)).ConvertFromInvariantString(rawValue.Value?.ToString());
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception($"Could not cast pin {inPin.Name} value: {rawValue.Key ?? "<unset>"}/{rawValue.Value ?? "<unset>"}", ex);
                }
            }

            // Log if inPin is null
            if ((value == null || value.Equals(default(T))) && inPin.DefaultValue != null)
            {
                var defaultValueType = inPin.DefaultValue?.GetType();
                var valueType        = typeof(T);

                /* ugly fix: if the value type is object and default value type is string,
                 * converter can not convert, so check if its object and string and just cast it.
                 */
                if (defaultValueType == valueType || (valueType == typeof(object) && defaultValueType == typeof(string)))
                {
                    value = (T)inPin.DefaultValue;
                }
                else if (inPin?.DefaultValue?.ToString() != null)
                {
                    value = (T)TypeDescriptor.GetConverter(valueType).ConvertFromInvariantString(inPin?.DefaultValue?.ToString());
                }
            }

            return(value);
        }
Exemple #2
0
        /// <summary>
        /// Get pin value as list
        /// </summary>
        /// <typeparam name="T">Generic type</typeparam>
        /// <param name="inPin">Pin instance</param>
        /// <returns>Result as list</returns>
        public IList <T> GetListValue <T>(DataPin inPin)
        {
            var pinKey = BuildPinHash(inPin.TemporaryNodeId, inPin.Id);

            if (PinValues.Any(x => x.Key == pinKey))
            {
                var value = (PinValues.FirstOrDefault(x => x.Key == pinKey).Value as IList);
                var list  = value.Cast <T>().ToList();

                // Check

                return(list);
            }
            else
            {
                return(new List <T>());
            }
        }