Example #1
0
        internal string ToString(bool typed)
        {
            if (m_argumentType == null)
            {
                return(base.ToString() !);
            }

            if (ArgumentType.IsEnum)
            {
                return(typed ? $"{Value}" : $"({ArgumentType.FullName}){Value}");
            }

            if (Value == null)
            {
                return(typed ? "null" : $"({ArgumentType.Name})null");
            }

            if (ArgumentType == typeof(string))
            {
                return($"\"{Value}\"");
            }

            if (ArgumentType == typeof(char))
            {
                return($"'{Value}'");
            }

            if (ArgumentType == typeof(Type))
            {
                return($"typeof({((Type)Value!).FullName})");
            }

            if (ArgumentType.IsArray)
            {
                IList <CustomAttributeTypedArgument> array = (IList <CustomAttributeTypedArgument>)Value !;
                Type elementType = ArgumentType.GetElementType() !;

                var result = new ValueStringBuilder(stackalloc char[256]);
                result.Append("new ");
                result.Append(elementType.IsEnum ? elementType.FullName : elementType.Name);
                result.Append('[');
                result.Append(array.Count.ToString());
                result.Append(']');

                for (int i = 0; i < array.Count; i++)
                {
                    if (i != 0)
                    {
                        result.Append(", ");
                    }
                    result.Append(array[i].ToString(elementType != typeof(object)));
                }

                result.Append(" }");

                return(result.ToString());
            }

            return(typed ? $"{Value}" : $"({ArgumentType.Name}){Value}");
        }
Example #2
0
        internal string ToString(bool typed)
        {
            if (ArgumentType == null)
            {
                return(base.ToString()); // Someone called ToString() on "default(CustomAttributeTypedArgument)"
            }
            try
            {
                if (ArgumentType.IsEnum)
                {
                    return(string.Format(CultureInfo.CurrentCulture, typed ? "{0}" : "({1}){0}", Value, ArgumentType.FullName));
                }

                else if (Value == null)
                {
                    return(string.Format(CultureInfo.CurrentCulture, typed ? "null" : "({0})null", ArgumentType.Name));
                }

                else if (ArgumentType == typeof(string))
                {
                    return(string.Format(CultureInfo.CurrentCulture, "\"{0}\"", Value));
                }

                else if (ArgumentType == typeof(char))
                {
                    return(string.Format(CultureInfo.CurrentCulture, "'{0}'", Value));
                }

                else if (ArgumentType == typeof(Type))
                {
                    return(string.Format(CultureInfo.CurrentCulture, "typeof({0})", ((Type)Value).FullName));
                }

                else if (ArgumentType.IsArray)
                {
                    string result = null;
                    IList <CustomAttributeTypedArgument> array = Value as IList <CustomAttributeTypedArgument>;

                    Type elementType = ArgumentType.GetElementType();
                    result = string.Format(CultureInfo.CurrentCulture, @"new {0}[{1}] {{ ", elementType.IsEnum ? elementType.FullName : elementType.Name, array.Count);

                    for (int i = 0; i < array.Count; i++)
                    {
                        result += string.Format(CultureInfo.CurrentCulture, i == 0 ? "{0}" : ", {0}", array[i].ToString(elementType != typeof(object)));
                    }

                    return(result += " }");
                }

                return(string.Format(CultureInfo.CurrentCulture, typed ? "{0}" : "({1}){0}", Value, ArgumentType.Name));
            }
            catch (MissingMetadataException)
            {
                return(base.ToString()); // Failsafe. Code inside "try" should still strive to avoid trigging a MissingMetadataException as caught exceptions are annoying when debugging.
            }
        }
        internal string ToString(bool typed)
        {
            if (m_argumentType == null)
            {
                return(base.ToString());
            }

            if (ArgumentType.IsEnum)
            {
                return(string.Format(typed ? "{0}" : "({1}){0}", Value, ArgumentType.FullName));
            }

            else if (Value == null)
            {
                return(string.Format(typed ? "null" : "({0})null", ArgumentType.Name));
            }

            else if (ArgumentType == typeof(string))
            {
                return(string.Format("\"{0}\"", Value));
            }

            else if (ArgumentType == typeof(char))
            {
                return(string.Format("'{0}'", Value));
            }

            else if (ArgumentType == typeof(Type))
            {
                return(string.Format("typeof({0})", ((Type)Value !).FullName));
            }

            else if (ArgumentType.IsArray)
            {
                IList <CustomAttributeTypedArgument> array = (IList <CustomAttributeTypedArgument>)Value !;

                Type   elementType = ArgumentType.GetElementType() !;
                string result      = string.Format(@"new {0}[{1}] {{ ", elementType.IsEnum ? elementType.FullName : elementType.Name, array.Count);

                for (int i = 0; i < array.Count; i++)
                {
                    result += string.Format(i == 0 ? "{0}" : ", {0}", array[i].ToString(elementType != typeof(object)));
                }

                result += " }";

                return(result);
            }

            return(string.Format(typed ? "{0}" : "({1}){0}", Value, ArgumentType.Name));
        }
Example #4
0
 internal string ToString(bool typed)
 {
     if (Value == null)
     {
         return(typed ? "null" : "(" + ArgumentType?.Name + ")null");
     }
     if (ArgumentType.IsEnum)
     {
         return(typed ? Value.ToString() : "(" + ArgumentType.FullName + ")" + Value.ToString());
     }
     if (Value is string s)
     {
         return("\"" + s + "\"");
     }
     if (Value is char c)
     {
         return("'" + c.ToString() + "'");
     }
     if (Value is DmdType type)
     {
         return("typeof(" + type.FullName + ")");
     }
     if (ArgumentType.IsArray)
     {
         var list        = (IList <DmdCustomAttributeTypedArgument>)Value;
         var elementType = ArgumentType.GetElementType();
         var sb          = ObjectPools.AllocStringBuilder();
         sb.Append("new ");
         sb.Append(elementType.IsEnum ? elementType.FullName : elementType.Name);
         sb.Append('[');
         sb.Append(list.Count);
         sb.Append("] { ");
         for (int i = 0; i < list.Count; i++)
         {
             if (i != 0)
             {
                 sb.Append(", ");
             }
             sb.Append(list[i].ToString(elementType != elementType.AppDomain.System_Object));
         }
         sb.Append(" }");
         return(ObjectPools.FreeAndToString(ref sb));
     }
     return(typed ? Value.ToString() : "(" + ArgumentType.Name + ")" + Value.ToString());
 }
        internal CommandUsageOverloadArgument(IClient client, ParameterInfo parameterInfo)
        {
            ArgumentType = parameterInfo.ParameterType;
            var innerType      = ArgumentType.GetElementType() ?? ArgumentType;
            var underlyingType = Nullable.GetUnderlyingType(innerType);

            Client    = client;
            Name      = parameterInfo.Name !;
            Type      = underlyingType ?? innerType;
            Optional  = underlyingType != null || parameterInfo.HasDefaultValue;
            Resolver  = Client.Resolvers[Type];
            Default   = parameterInfo.DefaultValue;
            Repeating = ArgumentType.IsArray;

            var attribute = parameterInfo.GetCustomAttribute <ArgumentAttribute>();

            Rest          = attribute?.Rest ?? false;
            Minimum       = attribute?.Minimum ?? int.MinValue;
            Maximum       = attribute?.Maximum ?? int.MaxValue;
            MinimumValues = attribute?.MinimumValues ?? CalculatedMinimumValues;
            MaximumValues = attribute?.MaximumValues ?? uint.MaxValue;
        }