Source generation: working with attributes with arguments of type System.Type

50 Views Asked by At

In my source generator project I have an attribute which takes an argument of type System.Type. I am able to get the name of the type that was passed in as string. I wonder if it is possible to get the actual instance of System.Type inside the generator?

I am using IIncrementalGenerator and context.SyntaxProvider.ForAttributeWithMetadataName to get the actual target nodes. In the second lambda while doing the transform I am trying to get hold of the System.Type instance.

Sample code:

public void Initialize(IncrementalGeneratorInitializationContext context)
{
    context.SyntaxProvider.ForAttributeWithMetadataName("Name.Of.AttributeAttribute", 
    (node, ct) => //filter logic
    (generatoraAttributeSyntaxContext, CancellationToken ct) =>
    {
        var nameOfPassedType = generatoraAttributeSyntaxContext.TargetSymbol.GetAttributes()[0].ConstructorArguments[0].Value.ToString()
        //Some further code
    }
}

I would like to have something like this:

public void Initialize(IncrementalGeneratorInitializationContext context)
{
    context.SyntaxProvider.ForAttributeWithMetadataName("Name.Of.AttributeAttribute", 
    (node, ct) => //filter logic
    (generatoraAttributeSyntaxContext, CancellationToken ct) =>
    {
        System.Type passedType = generatoraAttributeSyntaxContext. //Some further logic to actually acquire the type 
        //Some further code
    }
}

Code snippets assume that the attribute and argument are at given positions for testing purposes.

0

There are 0 best solutions below