Ejemplo n.º 1
0
        public static ConstantDescriptor Create(RosTypeInfo typeInfo, string identifier, object value)
        {
            if (typeInfo == null)
            {
                throw new ArgumentNullException(nameof(typeInfo));
            }
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (typeInfo.IsArray || !typeInfo.IsBuiltInType)
            {
                throw new InvalidOperationException($"Type {typeInfo} is not supported for constant declaration");
            }

            var typeMapping = BuiltInTypeMapping.Create(typeInfo);

            if (typeMapping.Type != value.GetType())
            {
                // Fix value type
                var converter = System.ComponentModel.TypeDescriptor.GetConverter(value.GetType());

                if (converter.CanConvertTo(typeMapping.Type))
                {
                    value = converter.ConvertTo(value, typeMapping.Type);
                }
                else
                {
                    throw new InvalidOperationException($"Cannot convert from {value.GetType()} to {typeMapping.Type}");
                }
            }

            return(new ConstantDescriptor(typeInfo, identifier, value));
        }
Ejemplo n.º 2
0
 protected bool Equals(BuiltInTypeMapping other)
 {
     return(Equals(Type, other.Type) && string.Equals(RosType, other.RosType));
 }