public static void BuildPropertyEquality(AttributesMetadata attributesMetadata, StringBuilder sb, IPropertySymbol property, bool explicitMode) { if (property.HasAttribute(attributesMetadata.IgnoreEquality)) { return; } var propertyName = property.ToFQF(); var typeName = property.Type.ToNullableFQF(); if (property.HasAttribute(attributesMetadata.UnorderedEquality)) { var types = property.GetIDictionaryTypeArguments(); if (types != null) { sb.AppendLine( $"&& global::Generator.Equals.DictionaryEqualityComparer<{string.Join(", ", types.Value)}>.Default.Equals({propertyName}!, other.{propertyName}!)"); } else { types = property.GetIEnumerableTypeArguments() !; sb.AppendLine( $"&& global::Generator.Equals.UnorderedEqualityComparer<{string.Join(", ", types.Value)}>.Default.Equals({propertyName}!, other.{propertyName}!)"); } } else if (property.HasAttribute(attributesMetadata.OrderedEquality)) { var types = property.GetIEnumerableTypeArguments() !; sb.AppendLine( $"&& global::Generator.Equals.OrderedEqualityComparer<{string.Join(", ", types.Value)}>.Default.Equals({propertyName}!, other.{propertyName}!)"); } else if (property.HasAttribute(attributesMetadata.ReferenceEquality)) { sb.AppendLine( $"&& global::Generator.Equals.ReferenceEqualityComparer<{typeName}>.Default.Equals({propertyName}!, other.{propertyName}!)"); } else if (property.HasAttribute(attributesMetadata.SetEquality)) { var types = property.GetIEnumerableTypeArguments() !; sb.AppendLine( $"&& global::Generator.Equals.SetEqualityComparer<{string.Join(", ", types.Value)}>.Default.Equals({propertyName}!, other.{propertyName}!)"); } else if (property.HasAttribute(attributesMetadata.CustomEquality)) { var attribute = property.GetAttribute(attributesMetadata.CustomEquality); var comparerType = (INamedTypeSymbol)attribute?.ConstructorArguments[0].Value !; var comparerMemberName = (string)attribute?.ConstructorArguments[1].Value !; if (comparerType.GetMembers().Any(x => x.Name == comparerMemberName && x.IsStatic) || comparerType.GetProperties().Any(x => x.Name == comparerMemberName && x.IsStatic)) { sb.AppendLine( $"&& {comparerType.ToFQF()}.{comparerMemberName}.Equals({propertyName}!, other.{propertyName}!)"); } else { sb.AppendLine( $"&& new {comparerType.ToFQF()}().Equals({propertyName}!, other.{propertyName}!)"); } } else if ( !explicitMode || property.HasAttribute(attributesMetadata.DefaultEquality)) { sb.AppendLine( $"&& global::System.Collections.Generic.EqualityComparer<{typeName}>.Default.Equals({propertyName}!, other.{propertyName}!)"); } }
public static object GetAttribute(this IPropertySymbol symbol, Type type, bool doNotTraverseHierarchy = false) { return(symbol.GetAttribute(type, MemberTypes.Property)); }