예제 #1
0
        private string GetWrite(string sourceName, string sourceCode, ITypeSymbol sourceType, string format, IValueSerializationGeneratorContext context)
        {
            var formatParameter = format.IsNullOrWhiteSpace()
                                ? string.Empty
                                : $"\"{format}\", ";
            var cultureParameter = sourceType.GetDeclarationGenericFullName().Contains("TimeSpan") && formatParameter.IsNullOrEmpty()
                                ? string.Empty
                                : "System.Globalization.CultureInfo.InvariantCulture";

            if (_supportedTypes.Contains(sourceType.GetDeclarationGenericFullName()))
            {
                return(context.Write <string>(sourceName, $"{sourceCode}.ToString({formatParameter} {cultureParameter})"));
            }
            else if (sourceType.IsNullable(out sourceType) && _supportedTypes.Contains(sourceType.GetDeclarationGenericFullName()))
            {
                var value = VariableHelper.GetName(sourceType);
                return($@"
					{sourceType.GetDeclarationGenericFullName()}? {value} = {sourceCode};
					if ({value}.HasValue)
					{{
						{context.Write<string>(sourceName, $"{value}.Value.ToString({formatParameter} {cultureParameter})")}
					}}"                    );
            }

            return(null);
        }
예제 #2
0
        public string GetWrite(string sourceName, string source, IPropertySymbol sourceProperty, IValueSerializationGeneratorContext context)
        {
            if (sourceProperty.FindAttributeByShortName("MicrosoftDateTimeFormatAttribute") != null)
            {
                var type = sourceProperty.Type;
                if (_supportedTypes.Contains(type.GetDeclarationGenericFullName()))
                {
                    return(context.Write <string>(sourceName, $"GeneratedSerializers.MicrosoftDateTimeHelper.ToString({source}.{sourceProperty.Name})"));
                }
                else if (type.IsNullable(out type) && _supportedTypes.Contains(type.GetDeclarationGenericFullName()))
                {
                    var value = VariableHelper.GetName(sourceProperty.Type);
                    return($@"
						var {value} = {source}.{sourceProperty.Name};
						if ({value}.HasValue)
						{{
							{context.Write<string>(sourceName, $"GeneratedSerializers.MicrosoftDateTimeHelper.ToString({value}.Value)")}
						}}"                        );
                }
                else
                {
                    return($"\r\n #error {_nonSupportedTypeError}");
                }
            }
            else
            {
                return(null);
            }
        }
        public string GetWrite(string sourceName, string sourceCode, ITypeSymbol sourceType, IValueSerializationGeneratorContext context)
        {
            if (_enums.IsResolvable(sourceType))
            {
                return(context.Write <string>(sourceName, $"{Enums.GetConverterName(sourceType)}.ToString({sourceCode})"));
            }

            return(null);
        }
예제 #4
0
 public override string Write(string sourceName, string sourceCode, bool isNullable, IValueSerializationGeneratorContext context)
 {
     return(context.Write <string>(sourceName, isNullable
                         ? $"{sourceCode}?.ToString(\"D\")"
                         : $"{sourceCode}.ToString(\"D\")"));
 }
예제 #5
0
 public override string Write(string sourceName, string sourceCode, bool isNullable, IValueSerializationGeneratorContext context)
 {
     return(context.Write <string>(sourceName, $"{sourceCode}?.{nameof(Uri.OriginalString)}"));
 }