コード例 #1
0
        private static string ArgumentToString(ArgumentPropertyAttribute attribute, PropertyInfo property, object value)
        {
            bool isRequired = attribute.PropertyType == ArgumentPropertyType.Required;
            bool isSwitch   = attribute.PropertyType == ArgumentPropertyType.Switch;

            if (value == null)
            {
                if (isRequired)
                {
                    throw new TaupoArgumentException(string.Format(CultureInfo.CurrentCulture, "Missing value for required argument {0}.", property.Name));
                }

                return(null);
            }

            if (property.PropertyType.GetBaseType() == typeof(Enum))
            {
                if (!isRequired && (int)value == 0)
                {
                    return(null);
                }
            }

            if (value is bool)
            {
                if (isSwitch && (bool)value == false)
                {
                    return(null);
                }
            }

            StringBuilder sb = new StringBuilder();

            var collectionValue = value as ICollection;

            if (collectionValue != null)
            {
                var  enumerator = collectionValue.GetEnumerator();
                bool first      = true;
                while (enumerator.MoveNext())
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        sb.Append(' ');
                    }

                    sb.Append(ArgumentToString(attribute, property, enumerator.Current));
                }

                return(sb.ToString());
            }

            sb.Append(attribute.ArgumentName);

            if (!isSwitch)
            {
                sb.Append(QuoteString(value.ToString()));
            }

            return(sb.ToString());
        }
コード例 #2
0
        private static string ArgumentToString(ArgumentPropertyAttribute attribute, PropertyInfo property, object value)
        {
            bool isRequired = attribute.PropertyType == ArgumentPropertyType.Required;
            bool isSwitch = attribute.PropertyType == ArgumentPropertyType.Switch;

            if (value == null)
            {
                if (isRequired)
                {
                    throw new TaupoArgumentException(string.Format(CultureInfo.CurrentCulture, "Missing value for required argument {0}.", property.Name));
                }

                return null;
            }

            if (property.PropertyType.GetBaseType() == typeof(Enum))
            {
                if (!isRequired && (int)value == 0)
                {
                    return null;
                }
            }

            if (value is bool)
            {
                if (isSwitch && (bool)value == false)
                {
                    return null;
                }
            }

            StringBuilder sb = new StringBuilder();

            var collectionValue = value as ICollection;
            if (collectionValue != null)
            {
                var enumerator = collectionValue.GetEnumerator();
                bool first = true;
                while (enumerator.MoveNext())
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        sb.Append(' ');
                    }

                    sb.Append(ArgumentToString(attribute, property, enumerator.Current));
                }

                return sb.ToString();
            }

            sb.Append(attribute.ArgumentName);

            if (!isSwitch)
            {
                sb.Append(QuoteString(value.ToString()));
            }

            return sb.ToString();
        }