postsharp / Metalama

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

Attribute.NamedArguments is always empty #248

Closed jonyadamit closed 11 months ago

jonyadamit commented 11 months ago

I am using version 2024.0.3-preview. I need to retrieve simple attributes decorating parameters of a method. My code looks somewhat like this:


var field = parameter.Attributes.OfAttributeType(typeof(ActivityFieldAttribute)).FirstOrDefault();
if (field != null)
{
    if (field.NamedArguments.TryGetValue(nameof(ActivityFieldAttribute.ResolveWith), out var resolveWithConstant))
        resolver = (string?)resolveWithConstant.Value;

    string fieldName = parameter.Name;
    if (field.NamedArguments.TryGetValue(nameof(ActivityFieldAttribute.FieldName), out var fieldNameConstant))
        fieldName = (string?)fieldNameConstant.Value ?? parameter.Name;
}

The problem is that field.NamedArguments is always empty and TryGetValue always returns false. Is there a different approach for getting the information from those attributes?

PostSharpBot commented 11 months ago

Hello @jonyadamit, 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-34245.

gfraiteur commented 11 months ago

How is the attribute initialized? If the values as passed as constructor arguments (even if they are then assigned to a property), you won't see them as named arguments.

You might want to try TryConstruct, which will require you to mark your type ActivityFieldAttribute as [RunTimeOrCompileTime].

jonyadamit commented 11 months ago

I see. Thank you very much!