예제 #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);
        }
예제 #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>());
            }
        }
예제 #3
0
        public virtual void CreateDataPins()
        {
            foreach (var property in GetType().GetProperties().Where(x => x.PropertyType == typeof(DataPin)))
            {
                // Find attribute
                var attribute = property.GetCustomAttributes(true).FirstOrDefault(x => x is DataPinDefinitionAttribute) as DataPinDefinitionAttribute;

                if (attribute != null)
                {
                    var pin = new DataPin
                    {
                        Id              = Guid.Parse(attribute.Id),
                        ContainerType   = attribute.ContainerType,
                        DataType        = attribute.DataType,
                        TemporaryNodeId = this.Id
                    };

                    // Create instance
                    property.SetValue(this, pin);
                }
            }
        }
예제 #4
0
        /// <summary>
        /// Set pin value
        /// </summary>
        /// <param name="outPin">Pin instance</param>
        /// <param name="value">Pin value</param>
        public void SetValue(DataPin outPin, object value)
        {
            var pinKey = BuildPinHash(outPin.TemporaryNodeId, outPin.Id);

            PinValues[pinKey] = value;
        }