【发布时间】:2017-07-19 16:43:02
【问题描述】:
如果我有一些带有typeof 表达式的源代码,例如,在一个属性中,我应该将TypedConstant 的Value 属性转换为什么?我在任何地方都找不到此信息。
例如,我有如下源码:
class FooAttribute : Attribute
{
public FooAttribute(Type type) {}
}
[Foo(typeof(int))]
class Bar {}
我想在我的分析器中查看Foo 的论点:
[DiagnosticAnalyzer(LanguageNames.CSharp)]
class MyAnalyser : DiagnosticAnalyzer
{
public override void Initialize(AnalysisContext context)
{
context.RegisterCompilationAction(LookAtFooArg);
}
private void LookAtFooArg(CompilationAnalysisContext context)
{
TypedConstant argumentTypedConstant = context.Compilation
.GetTypeByMetadataName("Bar")
.GetAttributes()
.Single()
.ConstructorArguments
.Single();
// Value here is typed as an object. What should I cast it to?
var typeOfExpressionValue = argumentTypedConstant.Value;
}
}
【问题讨论】:
标签: c# roslyn typeof roslyn-code-analysis