postsharp / Metalama

Metalama is a Roslyn-based meta-programming framework. Use this repo to report bugs or ask questions.
176 stars 5 forks source link

Introduce attribute does not seem to be working #233

Closed sharinganthief closed 11 months ago

sharinganthief commented 11 months ago

i have the following aspect:

public class IgnoreAttribute : FieldOrPropertyAspect { [Template] public override void BuildAspect( IAspectBuilder<IFieldOrProperty> builder ) { builder.Advice.IntroduceAttribute(builder.Target, AttributeConstruction.Create(typeof(YamlIgnoreAttribute)));

base.BuildAspect(builder); } }

assigned like so:

[Ignore] public bool SomeProperty => SomeOtherProperty != 0;

but the property does not get the ignore attribute as seen with reflection at runtime or testing the serialization

I have tried builder.Target.Compilation, every meta.Target option there is i believe but nothing seems to work.

Am i doing this wrong?

PostSharpBot commented 11 months ago

Hello @sharinganthief, thank you for submitting this issue. We will try to get back to you as soon as possible. Note to the PostSharp team, this ticket is being tracked in our dashboard under ID TP-34033.

gfraiteur commented 11 months ago

The BuildAspect method should not have the [Template] attribute. I think it's the only issue in the code.

sharinganthief commented 11 months ago

Thanks for the reply!

Removed template, no change:

image image

The idea here is to not have to drop [YamlIgnore] [JsonIgnore] [XmlIgnore] on all ignored props across the sln, but a single ignore that gets al of them added

svick commented 11 months ago

It works fine for me:

I was using this code:

using Metalama.Framework.Aspects;
using Metalama.Framework.Code.DeclarationBuilders;
using Metalama.Framework.Code;
using YamlDotNet.Serialization;

public class IgnoreAttribute : FieldOrPropertyAspect
{
    public override void BuildAspect( IAspectBuilder<IFieldOrProperty> builder )
    {
        builder.Advice.IntroduceAttribute(builder.Target,
            AttributeConstruction.Create(typeof(YamlIgnoreAttribute)));

        base.BuildAspect(builder);
    }
}

class Target 
{
    [Ignore] public bool SomeProperty => SomeOtherProperty != 0;

    public int SomeOtherProperty { get; set; }
}

Can you share complete code that reproduces the problem?

sharinganthief commented 11 months ago

Huh. New sln, copied your code, works perfectly. old sln, uninstall all metalama nuget, reinstall latest, works. user error of some kind i guess. thanks for the assist