Beispiel #1
0
        // From Value to string (serialization)
        private string GetValuesMap(ITypeSymbol type)
        {
            var typeName = type.GetDeclarationGenericFullName();

            string[] values;
            if (type.FindAttribute("blabla") != null)
            {
                values = type
                         .GetMembers()
                         .OfType <IFieldSymbol>()
                         // If the enum have multiple fields with the same value (eg. BlaBlaEnum.Default = BlaBlaEnum.HellWorld), the generated dictionary will be valid
                         // and will throw a TypeInitializationException or ArgumentException. So keep only the first occurence.
                         .Distinct(FuncEqualityComparer <IFieldSymbol> .Create(field => field.ConstantValue))
                         .Select(field => $@"{{{typeName}.{field.Name}, ""{field.ConstantValue.ToString()}""}},")
                         .ToArray();
            }
            else
            {
                values = type
                         .GetMembers()
                         .OfType <IFieldSymbol>()
                         // If the enum have multiple fields with the same value (eg. BlaBlaEnum.Default = BlaBlaEnum.HellWorld), the generated dictionary will be valid
                         // and will throw a TypeInitializationException or ArgumentException. So keep only the first occurence.
                         .Distinct(FuncEqualityComparer <IFieldSymbol> .Create(field => field.ConstantValue))
                         .Select(field => $@"{{{typeName}.{field.Name}, ""{_codeAnalyzer.GetName(field)}""}},")
                         .ToArray();
            }

            return($@"new Dictionary<{typeName}, string>({values.Length})
				{{
					{values.JoinBy(Environment.NewLine)}
				}}"                );
        }
 private string GetSerializerFactories(SerializerInfo[] serializers)
 {
     return(serializers
            .Distinct(FuncEqualityComparer <SerializerInfo> .Create(info => info.Name))
            .Select(info => info.BuildSerializer())
            .Trim()
            .JoinBy(Environment.NewLine));
 }
Beispiel #3
0
        // From string to enum value (Deserialization)
        private string GetReversedValuesMap(ITypeSymbol type)
        {
            var typeName = type.GetDeclarationGenericFullName();

            var literalValues = type
                                .GetMembers()
                                .OfType <IFieldSymbol>()
                                .Select(field => $@"{{""{_codeAnalyzer.GetName(field)}"", {typeName}.{field.Name}}},");
            var underlyingTypeValues = type
                                       .GetMembers()
                                       .OfType <IFieldSymbol>()
                                       .Distinct(FuncEqualityComparer <IFieldSymbol> .Create(field => field.ConstantValue))
                                       .Select(field => $@"{{""{field.ConstantValue.ToString()}"", {typeName}.{field.Name}}},");

            var values = literalValues.Concat(underlyingTypeValues).ToArray();

            return($@"new Dictionary<string, {typeName}>({values.Length}, System.StringComparer.OrdinalIgnoreCase)
				{{
					{values.JoinBy(Environment.NewLine)}
				}}"                );
        }
 protected CollectionEqualityComparerBase([CanBeNull] IEqualityComparer <T> elementEqualityComparer)
 {
     ElementEqualityComparer = elementEqualityComparer ?? FuncEqualityComparer <T> .Create(AreEqualsFunc, GetHashCodeFunc);
 }