简体   繁体   中英

How to find the value of an attribute constructor argument using Roslyn?

Using Roslyn, how would I find the value of the constructor of an attribute? So given the following class with an attribute:

[Example(typeof(ClassFromAnotherDll))]
public class ExampleClass
{
    public int JustANumber { get; set; }
}

And the ExampleAttribute would look like so (though the source would not be in the same solution as the one above):

public class ExampleAttribute : Attribute
{
    private readonly Type _type;

    public ExampleAttribute(Type type)
    {
        _type = type;
    }
}

How would I get information (eg properties, constructors) about the ClassFromAnotherDll type?

Call GetAttributes() on any symbol (from the semantic model) to get a list of all applied attributes.

Then look at the ConstructorArguments of the attribute you want.

If the argument is a typeof expression, its Value will be an INamedTypeSymbol .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM