English 中文(简体)
来自ITypeSymbol的GetSyntax
原标题:Get TypeSyntax from ITypeSymbol
  • 时间:2011-11-22 17:41:24
  •  标签:
  • c#
  • roslyn

I m experimenting a bit with the Roslyn-CTP.

Currently I m trying to replace var with the concrete type.

var i=1;

应当成为:

int i=1;

简单明了所推断的类型。 但是,由于这一部分出现在语法模式中,因此,我收到了一份。 更换是在辛塔克斯模式中进行的,因此,我需要一个<条码>。 由于我不希望获得一个缩略语(global:System.Int32),转换的背景取决于(using, nested categories等)。

The Visual studio version that s part of Roslyn already has this functionality in its "Simplify type name" quickfix, but looking over the samples I couldn t find an easy way to do this conversion.


Based on Kevin Pilch-Bisson s answer I m now using:

var location = document.GetSyntaxTree().GetLocation(node);
string name = variableType.ToMinimalDisplayString((Location)location, (SemanticModel)document.GetSemanticModel());

A location which ToMinimalDisplayString can be obtained from a CommonSyntaxTree.

An additional complication is that ToMinimalDisplayString requires the classes Location and SemanticModel, whereas document.GetSemanticModel() and CommonSyntaxTree.GetLocation only return an interface.
I worked around by simply casting to the classes, which seems to work for now.

该校像是C#级,接口语言独立。


I ve uploaded a working edition on github:

It doesn t work for var in a foreach, but I suspect that s a limitation of the current Roslyn build.

最佳回答

您可以利用ToMinimalDisplayString(>的推广方法获得最简短的法律指示,该推广方法适用于ISymbol(注:见“Roslyn.Compilers.CSharp.SymbolDisplay”。

申斥: 我在微软公司罗斯林小组工作。

问题回答

问题: 提出可取消的论点类型。 它可以是习俗目标或系统识别。

在c#中,定制的正值为()=识别器具和装饰物/有预定义的类型。

这就是你如何获得可取消的那种论点。

[他的身份识别名指是习惯物体。 它可以被预先界定。

 Class A
 {
     public bool ? test;
    public b ? obj1;
 }
 // Get the semantic model and get the property declarationsyntax

 PropertyDeclarationSyntax prop = (get it somehow)[]

 INamedTypeSymbol tp = (INamedTypeSymbol)x.Type;

 string nameoftype = tp.TypeArguments.FirstOrDefault().Name;

 ITypeSymbol s1 = tp.TypeArguments.FirstOrDefault();

 TypeSyntax t1 = SyntaxFactory.ParseTypeName(s1.ToMinimalDisplayString(semanticModel, 
 prop.SpanStart));

 t1.kind == IdentifierName meaning its custom object type.




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签