我试图用Roslyn获得MyAttribute的命名参数。
var sourceCode = (@"
public class MyAttribute : Attribute
{
public string Test { get; set; }
}
[MyAttribute(Test = ""Hello"")]
public class MyClass { }
");
var syntaxTree = CSharpSyntaxTree.ParseText(sourceCode);
var mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
var compilation = CSharpCompilation.Create("MyCompilation", new[] { syntaxTree }, new[] { mscorlib });
var semanticModel = compilation.GetSemanticModel(syntaxTree);
var syntaxRoot = syntaxTree.GetRoot();
var classNode = syntaxRoot.DescendantNodes().OfType<ClassDeclarationSyntax>().Skip(1).First();
var classModel = (ITypeSymbol)semanticModel.GetDeclaredSymbol(classNode);
var firstAttribute = classModel.GetAttributes().First();但是,firstAttribute.AttributeClass.Kind等于ErrorType,因此firstAttribute.NamedArguments不包含任何元素。
代码不是anlyzer或者其他什么东西,我有更完整的上下文,比如解决方案。
我看不出罗斯林漏掉了什么推荐信什么的。我能做些什么来充分分析属性呢?
发布于 2018-03-06 11:02:06
您需要完全限定Attribute类型名称:
var sourceCode = (@"
public class MyAttribute : System.Attribute // < here
{
public string Test { get; set; }
}
[MyAttribute(Test = ""Hello"")]
public class MyClass { }
");然后,它将如您所期望的那样工作:
var firstNamedArg = firstAttribute.NamedArguments[0];
var key = firstNamedArg.Key; // "Test"
var value = firstNamedArg.Value.Value; // "Hello"或者,您可以在顶部添加using System;:
var sourceCode = (@"
using System;
public class MyAttribute : Attribute
{
public string Test { get; set; }
}
[MyAttribute(Test = ""Hello"")]
public class MyClass { }
");发布于 2018-03-20 20:29:47
与使用Roslyn的SemanticModel不同,您还可以简单地使用语法API获取atttribute的参数信息:
var firstAttribute = classNode.AttributeLists.First().Attributes.First();
var attributeName = firstAttribute.Name.NormalizeWhitespace().ToFullString();
Console.WriteLine(attributeName);
// prints --> "MyAttribute"
var firstArgument = firstAttribute.ArgumentList.Arguments.First();
var argumentFullString = firstArgument.NormalizeWhitespace().ToFullString();
Console.WriteLine(argumentFullString);
// prints --> Test = "Hello"
var argumentName = firstArgument.NameEquals.Name.Identifier.ValueText;
Console.WriteLine(argumentName);
// prints --> Test
var argumentExpression = firstArgument.Expression.NormalizeWhitespace().ToFullString();
Console.WriteLine(argumentExpression);
// prints --> "Hello"发布于 2021-12-26 11:47:50
一旦您拥有了您的xxxDeclaredSymbol,您就可以获得如下所有属性
var attributes = methodSymbol.GetAttributes().ToArray();然后可以循环遍历数组中的每个AttributeData,并使用我编写的扩展,它将给出传递给属性的构造函数的所有名称+值的列表,不管它们是否命名.
// Used as a 3 value tuple for Name + TypeName + actual value
public class NameTypeAndValue
{
public string Name { get; private set; }
public string TypeFullName { get; private set; }
public object Value { get; private set; }
public NameTypeAndValue(string name, string typeFullName, object value)
{
Name = name;
TypeFullName = typeFullName;
Value = value;
}
}
public static class ITypeSymbolExtensions
{
// Converts names like `string` to `System.String`
public static string GetTypeFullName(this ITypeSymbol typeSymbol) =>
typeSymbol.SpecialType == SpecialType.None
? typeSymbol.ToDisplayString()
: typeSymbol.SpecialType.ToString().Replace("_", ".");
}
public static bool TryGetAttributeAndValues(
this AttributeData attributeData,
string attributeFullName,
SemanticModel model,
out IEnumerable<NameTypeAndValue> attributeValues)
{
var attributeValuesList = new List<NameTypeAndValue>();
var constructorParams = attributeData.AttributeConstructor.Parameters;
// Start with an indexed list of names for mandatory args
var argumentNames = constructorParams.Select(x => x.Name).ToArray();
var allArguments = attributeData.ConstructorArguments
// For unnamed args, we get the name from the array we just made
.Select((info, index) => new KeyValuePair<string, TypedConstant>(argumentNames[index], info))
// Then we use name + value from the named values
.Union(attributeData.NamedArguments.Select(x => new KeyValuePair<string, TypedConstant>(x.Key, x.Value)))
.Distinct();
foreach(var argument in allArguments)
{
attributeValuesList.Add(
new NameTypeAndValue(
name: argument.Key,
typeFullName: argument.Value.Type.GetTypeFullName(),
value: argument.Value.Value));
}
attributeValues = attributeValuesList.ToArray();
return true;
}https://stackoverflow.com/questions/49129099
复制相似问题