I have an Attribute class used to decorate an assembly for use in C# source code generators. I am trying to get the value of one of the arguments that is type System.Type. I can get the values of the other properties just fine.
Here's the attribute class:
[AttributeUsage(AttributeTargets.Assembly)]
public class MigrationFunctionAttribute : Attribute
{
public Type MigrationFunction { get; set; }
public string MigrationMethod { get; set; }
public string DependsOn { get; set; }
public string SqlScriptBucket { get; set; }
// ATTRIBUTE: ADD HERE
public string Branch { get; set; }
}
Here's my usage of the attribute:
[assembly: MigrationFunction(MigrationFunction = typeof(MigrationFunctions), MigrationMethod = nameof(MigrationFunctions.MyMigrator), Branch = "@Branch", DependsOn = "Restore")]
After receiving the attribute in the SyntaxReceiver, I try to build a model from the information:
public static AttributeModel2<IMigrationFunctionAttributeModel> Build(AttributeSyntax receiverMigrationFunctionAttribute, GeneratorExecutionContext context)
{
SemanticModel semanticModel = context.Compilation.GetSemanticModel(receiverMigrationFunctionAttribute.SyntaxTree);
foreach (var attributeArgumentSyntax in receiverMigrationFunctionAttribute.ArgumentList.Arguments)
{
switch (attributeArgumentSyntax.NameEquals.Name.Identifier.ValueText)
{
case nameof(MigrationFunctionAttribute.MigrationFunction):
Debug.WriteLine(attributeArgumentSyntax.Expression);
// ^^^ outputs 'typeof(MigrationFunctions)'
TypeInfo t = semanticModel.GetTypeInfo(attributeArgumentSyntax.Expression);
Debug.WriteLine(t.Type.ToDisplayString());
// ^^^ outputs 'System.Type'
/// HOW DO I GET THE TYPE HERE, which should be "MigrationFunctions", not "System.Type"???
break;
case nameof(MigrationFunctionAttribute.Branch):
var value = semanticModel.GetConstantValue(attributeArgumentSyntax.Expression);
Debug.WriteLine(value.Value);
// ^^^ outputs '@Branch just fine
break;
}
}
return null;
}
The model builder method can pick through the arguments as expected, but I cannot figure out how to get the correct type info for the property:
public Type MigrationFunction { get; set; }
I get System.Type, whereas I am expecting to get MigrationFunctions.
When I output from, I get typeof(MigrationFunctions):
Debug.WriteLine(attributeArgumentSyntax.Expression);
As documented in the code, HOW DO I GET THE TYPE HERE, which should be "MigrationFunctions", not "System.Type"???