private string GetDefaultValue(ParameterSignature parameter, TypeEmitter writer) { if (!parameter.IsOptional) { return(string.Empty); } if (parameter.DefaultValue == null) { return("null"); } var type = parameter.DefaultValue.GetType(); var value = parameter.DefaultValue; // Nullable? if (parameter.ParameterType.Namespace.Name == "System" && parameter.ParameterType.Name == "Nullable") { // This is really only needing to account for char? and bool? // Unwrap the type and use the same logic as non-nullable by calling the BuildParameterValueToken method. var innerType = parameter.ParameterType.GenericParameters[0]; return($"({writer.GetString(innerType)}){BuildParameterValueToken(type, value)}"); } // Enum or numeric? if (parameter.ParameterType.IsEnum || _numericTypes.Contains(type)) { // Nullable numerics are handled in the previous block, so just cast it and use the value. return(string.Format(CultureInfo.InvariantCulture, "({0}){1}", writer.GetString(parameter.ParameterType), value)); } return(BuildParameterValueToken(type, value)); }
private IEnumerable <string> GetParameterTokens(ParameterSignature parameter, ParameterEmitOptions options) { if (parameter.IsOutParameter) { if (options.HasFlag(ParameterEmitOptions.Keywords)) { yield return("out"); } } else if (parameter.IsRefParameter) { if (options.HasFlag(ParameterEmitOptions.Keywords)) { yield return("ref"); } } if (!options.HasFlag(ParameterEmitOptions.Invocation)) { if (parameter.IsParams) { yield return("params"); } yield return(_typeEmitter.GetString(parameter.ParameterType)); } if (options.HasFlag(ParameterEmitOptions.Name)) { yield return(_keywords.Contains(parameter.Name) ? $"@{parameter.Name}" : parameter.Name); } if (parameter.IsOptional) { if (!options.HasFlag(ParameterEmitOptions.Invocation) && options.HasFlag(ParameterEmitOptions.Optional)) { yield return("="); yield return(GetDefaultValue(parameter, _typeEmitter)); } } }