获取一个类型的完全限定名从TypeInfo对象 [英] Getting the fully qualified name of a type from a TypeInfo object
问题描述
是它在某种程度上可能得到包含在所属类别
对象类型的全名?
Is it somehow possible to get the fully qualified name of the type contained in a TypeInfo
object?
在调试许多价值观很好地显示为 System.Int32
但是当它打印出来,他们没有一个包含此完全合格的名称。我想这能提供作为参数传递给 Type.GetType()
。
In the debugger many of these values nicely show up as System.Int32
but when it's printed out, not one of them contains this fully qualified name. I need this to provide as an argument to Type.GetType()
.
var typeInfo = semanticModel.GetTypeInfo(argument);
var w = typeInfo.ToString(); // Microsoft.CodeAnalysis.TypeInfo
var y = typeInfo.Type.ToString(); // int
var z = typeInfo.Type.ToDisplayString(); // int
var a = typeInfo.Type.OriginalDefinition.ToDisplayString(); // int
var b = typeInfo.Type.OriginalDefinition.ToString(); // int
var c = typeInfo.Type.Name; // Int32
var d = typeInfo.Type.MetadataName; // Int32
var e = typeInfo.Type.ToDisplayParts(); // {int}
var f = typeInfo.Type.ContainingNamespace; // System
请注意,这应该对任何类型的工作,所以我不能只是连接具有空间这个名字
Note that this should work for every type so I can't just concatenate the namespace with the name.
另外:有另一种(?更适合)的方式得到确切类型
Alternatively: is there another (more suited?) way to get the exact type?
上下文:我要检查,如果一个类的类型参数包含一些具体的方法。因此,我的方法是从 TypeArgumentListSyntax
得到的参数,并获得所属类别
每个 TypeSyntax
对象。
For context: I want to check if the type-parameters of a class contain a few specific methods. Therefore my approach was to get the parameters from the TypeArgumentListSyntax
and get the TypeInfo
from each TypeSyntax
object.
推荐答案
的 ToDisplayString
方法,您可以在格式对象传递其中有一个巨大的,你怎么想格式化的东西控制选项编号:
The ToDisplayString
method lets you pass in a "format" object which has a huge number of options for controlling how you want to format stuff:
var symbolDisplayFormat = new SymbolDisplayFormat(
typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces);
string fullyQualifiedName = typeSymbol.ToDisplayString(symbolDisplayFormat);
原因你喜欢INT获得的关键字是默认的格式包括 SymbolDisplayMiscellaneousOptions.UseSpecialTypes
标志,指定用于特殊类型与正规名称的语言关键字。
The reason your getting keywords like "int" is the default format is including the SymbolDisplayMiscellaneousOptions.UseSpecialTypes
flag which specifies to use the language keywords for special types vs. the regular name.
这篇关于获取一个类型的完全限定名从TypeInfo对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!