Beispiel #1
0
        static string valueToScpiString(object propertyValue)
        {
            var    type = propertyValue.GetType();
            string value;

            if (type == typeof(bool))
            {
                value = (bool)propertyValue ? "ON" : "OFF";
            }
            else if (type.IsEnum)
            {
                value = ScpiEnum.GetEnumConv(type).ToString((Enum)propertyValue);
            }
            else if (type.IsArray)
            {
                var Props = (Array)propertyValue;

                string[] PropStrings = new string[Props.Length];
                for (int i = 0; i < Props.Length; i++)
                {
                    PropStrings[i] = valueToScpiString(Props.GetValue(i));
                }

                value = string.Join(",", PropStrings);
            }
            else
            {
                value = Convert.ToString(propertyValue, CultureInfo.InvariantCulture);
            }

            return(value);
        }
Beispiel #2
0
 /// <summary>
 /// Get an enum converter for a specific enum type.
 /// </summary>
 /// <param name="t">Must be a enum type!</param>
 /// <returns></returns>
 public static ScpiEnum GetEnumConv(Type t)
 {
     if (converters.ContainsKey(t) == false)
     {
         converters[t] = new ScpiEnum(t);
     }
     return(converters[t]);
 }
Beispiel #3
0
        /// <summary>
        /// Overloaded.  Parses the result of a SCPI query back to T, with special parsing for enums, bools and arrays. Bools support 1/0 and ON/OFF formats.
        /// If Enums are tagged with <see cref="ScpiAttribute"/>, <see cref="ScpiAttribute.ScpiString"/> will be used instead of <see cref="string.ToString()"/> .
        /// </summary>
        /// <param name="scpiString"></param>
        /// <param name="T"></param>
        /// <returns></returns>
        public static object Parse(string scpiString, Type T)
        {
            if (scpiString == null)
            {
                throw new ArgumentNullException("scpiString");
            }
            if (T == null)
            {
                throw new ArgumentNullException("T");
            }
            scpiString = scpiString.Trim(); // Ensure no garbage.
            if (T == typeof(bool))
            {
                return((object)(scpiString == "ON" || scpiString == "1"));
            }
            else if (T.IsEnum)
            {
                return((object)ScpiEnum
                       .GetEnumConv(T)
                       .FromString(scpiString));
            }
            else if (T.IsArray)
            {
                List <string> elements = SplitScpiArray(scpiString);
                Array         result   = Array.CreateInstance(T.GetElementType(), elements.Count);

                for (int i = 0; i < elements.Count; i++)
                {
                    result.SetValue(Parse(elements[i], T.GetElementType()), i);
                }

                return(result);
            }

            return(Convert.ChangeType(scpiString, T, CultureInfo.InvariantCulture));
        }