diff --git a/AutoInterfaceSample/AutoInterfaceSample.csproj b/AutoInterfaceSample/AutoInterfaceSample.csproj index 7f194bb..719528d 100644 --- a/AutoInterfaceSample/AutoInterfaceSample.csproj +++ b/AutoInterfaceSample/AutoInterfaceSample.csproj @@ -16,7 +16,9 @@ + + diff --git a/AutoInterfaceSample/Program.cs b/AutoInterfaceSample/Program.cs index 4a06fe8..76d9cbb 100644 --- a/AutoInterfaceSample/Program.cs +++ b/AutoInterfaceSample/Program.cs @@ -1,10 +1,14 @@ -using TestInterfaces.A.B; +using System; +using System.Diagnostics.CodeAnalysis; namespace AutoInterfaceSample.Test { - public partial record TestRecord([property: BeaKona.AutoInterface(IncludeBaseInterfaces = true)] ITestable Testable) - { - } + public partial record TestRecord( + [property: BeaKona.AutoInterface(IncludeBaseInterfaces = true)] NLog.ILogger nlog, + [property: BeaKona.AutoInterface(IncludeBaseInterfaces = true)] Serilog.ILogger slog, + [property: BeaKona.AutoInterface(IncludeBaseInterfaces = true)] Microsoft.Extensions.Logging.ILogger melog + + ) : NLog.ILogger; public class Program { @@ -12,12 +16,37 @@ public static void Main() { //System.Diagnostics.Debug.WriteLine(BeaKona.Output.Debug_TestRecord.Info); - ITestable p = new TestRecord(new SimpleLogger()); + // ITestable p = new TestRecord(new SimpleLogger()); - p.Test(); + // p.Test(); //p.Log(LogLevel.Debug, default, 1, null, null); //var result = p.BindProperty("test", 1, false, out var property, 1, 2, 3); } } + + partial record TestObsoleteRecord( + [property: BeaKona.AutoInterface(IncludeBaseInterfaces = true)] + ITestInterfaceObsolete Testable) : ITestInterfaceObsolete; + interface ITestInterfaceObsolete + { + [Obsolete] + [return: System.Diagnostics.CodeAnalysis.MaybeNull] + [return: NotNullIfNotNullAttribute("test")] + [return: TestReturnAttribute] + ResObject? TestObsoleteMethod(string? test); + + [Obsolete] + [System.Diagnostics.CodeAnalysis.DisallowNull] + ResObject? TestObsoleteProperty { get; } + + + } + + [Obsolete] + + class ResObject; + + [AttributeUsage(AttributeTargets.All)] + class TestReturnAttribute : Attribute; } diff --git a/BeaKona.AutoInterfaceGenerator/CSharpCodeTextWriter.cs b/BeaKona.AutoInterfaceGenerator/CSharpCodeTextWriter.cs index fc6a618..19f9afd 100644 --- a/BeaKona.AutoInterfaceGenerator/CSharpCodeTextWriter.cs +++ b/BeaKona.AutoInterfaceGenerator/CSharpCodeTextWriter.cs @@ -413,6 +413,25 @@ private IEnumerable GetForwardAttributes(ISymbol symbol) } } + private IEnumerable GetReturnAttributes(IPropertySymbol method) + { + var attributes = method.GetAttributes().Where(IsPublicAccess).ToList(); + + foreach (var attribute in attributes) + { + if (attribute.AttributeClass is INamedTypeSymbol attributeClass) + { + foreach (var typeSymbol in this.returnAttributeSymbols) + { + if (attributeClass.Equals(typeSymbol, SymbolEqualityComparer.Default)) + { + yield return attribute; + break; + } + } + } + } + } private IEnumerable GetReturnAttributes(IMethodSymbol method) { var attributes = method.GetReturnTypeAttributes().Where(IsPublicAccess).ToList(); @@ -654,7 +673,7 @@ public void WritePropertyDefinition(SourceBuilder builder, IPropertySymbol prope PartialTemplate? setterTemplate = this.GetMatchedTemplates(references, setterTarget, property.IsIndexer ? "this" : property.Name); this.WriteForwardAttributes(builder, property); - this.WriteReturnAttributes(builder, property.Type.GetAttributes()); + this.WriteReturnAttributes(builder, GetReturnAttributes(property)); builder.AppendIndentation(); this.WriteTypeReference(builder, property.Type, scope);