/// <summary> /// Make the struct a strongly typed ID /// </summary> /// <param name="generateJsonConverter">If true generates a JsonConverter for the strongly typed ID (requires a reference to Newtonsoft.Json in the project)</param> /// <param name="backingType">The <see cref="Type"/> to use to store the strongly-typed ID value. Defaults to <see cref="StronglyTypedIdBackingType.Guid"/></param> /// <param name="jsonConverter">JSON library used to serialize/deserialize strongly-typed ID value. Defaults to <see cref="StronglyTypedIdJsonConverter.NewtonsoftJson"/></param> public StronglyTypedIdAttribute( bool generateJsonConverter = true, StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Guid, StronglyTypedIdJsonConverter jsonConverter = StronglyTypedIdJsonConverter.NewtonsoftJson) { GenerateJsonConverter = generateJsonConverter; BackingType = backingType; JsonConverter = jsonConverter; }
public StronglyTypedIdGenerator(AttributeData attributeData) { if (attributeData == null) { throw new ArgumentNullException(nameof(attributeData)); } _generateJsonConverter = (bool)attributeData.ConstructorArguments[0].Value; _backingType = (StronglyTypedIdBackingType)attributeData.ConstructorArguments[1].Value; _jsonProvider = (StronglyTypedIdJsonConverter)attributeData.ConstructorArguments[2].Value; }
// Generated using https://roslynquoter.azurewebsites.net/ public SyntaxList <MemberDeclarationSyntax> CreateStronglyTypedIdSyntax(StructDeclarationSyntax original, bool generateJsonConverter, StronglyTypedIdJsonConverter jsonProvider) { // Get the name of the decorated member var idName = original.Identifier.ValueText; var typeConverterName = idName + "TypeConverter"; var attributes = new List <AttributeListSyntax>() { GetTypeConverterAttribute(typeConverterName) }; var members = GetMembers(idName).Append(GetTypeConverter(typeConverterName, idName)); if (generateJsonConverter) { if (jsonProvider.HasFlag(StronglyTypedIdJsonConverter.NewtonsoftJson)) { var jsonConverterName = idName + "NewtonsoftJsonConverter"; attributes.Add(GetNewtonsoftJsonConverterAttribute(jsonConverterName)); members = members.Append(GetNewtonsoftJsonConverter(jsonConverterName, idName)); } if (jsonProvider.HasFlag(StronglyTypedIdJsonConverter.SystemTextJson)) { var jsonConverterName = idName + "SystemTextJsonConverter"; attributes.Add(GetSystemTextJsonConverterAttribute(jsonConverterName)); members = members.Append(GetSystemTextJsonConverter(jsonConverterName, idName)); } } return(SingletonList <MemberDeclarationSyntax>( StructDeclaration(idName) .WithAttributeLists(List <AttributeListSyntax>(attributes)) .WithModifiers( TokenList( new[] { Token(SyntaxKind.ReadOnlyKeyword), Token(SyntaxKind.PartialKeyword) })) .WithBaseList( BaseList( SeparatedList <BaseTypeSyntax>( new SyntaxNodeOrToken[] { SimpleBaseType( QualifiedName( IdentifierName("System"), GenericName( Identifier("IComparable")) .WithTypeArgumentList( TypeArgumentList( SingletonSeparatedList <TypeSyntax>( IdentifierName(idName)))))), Token(SyntaxKind.CommaToken), SimpleBaseType( QualifiedName( IdentifierName("System"), GenericName( Identifier("IEquatable")) .WithTypeArgumentList( TypeArgumentList( SingletonSeparatedList <TypeSyntax>( IdentifierName(idName)))))) }))) .WithMembers(List <MemberDeclarationSyntax>(members)) .WithCloseBraceToken( Token( TriviaList(), SyntaxKind.CloseBraceToken, TriviaList())))); }
protected AttributeListSyntax GetJsonConverterAttribute(string jsonConverterName, StronglyTypedIdJsonConverter jsonProvider) { switch (jsonProvider) { case StronglyTypedIdJsonConverter.SystemTextJson: return(GetSystemTextJsonConverterAttribute(jsonConverterName)); case StronglyTypedIdJsonConverter.NewtonsoftJson: default: return(GetNewtonsoftJsonConverterAttribute(jsonConverterName)); } }
protected ClassDeclarationSyntax GetJsonConverter(string jsonConverterName, string idName, StronglyTypedIdJsonConverter jsonProvider) { switch (jsonProvider) { case StronglyTypedIdJsonConverter.SystemTextJson: return(GetSystemTextJsonConverter(jsonConverterName, idName)); case StronglyTypedIdJsonConverter.NewtonsoftJson: default: return(GetNewtonsoftJsonConverter(jsonConverterName, idName)); } }