How to create a Roslyn ITypeSymbol for an arbitrary type?

隐身守侯 提交于 2021-02-07 08:27:50

问题


I can use a SyntaxGenerator to generate a parameter of type Int32 like so...

var generator = SyntaxGenerator.GetGenerator(document);
var paramType = generator.TypeExpression(SpecialType.System_Int32);
var param = generator.ParameterDeclaration("MyParam", paramType);

What equivalent code should I use to create a parameter of type Dataset?

I presume I need to create an ITypeSymbol to pass to the generator.TypeExpression, but how to do this?


回答1:


If you have access to a Compilation, you can use GetTypeByMetadataName, as explained in this blog post and this SO answer:

var dataSetType = compilation.GetTypeByMetadataName("System.Data.DataSet");
var paramType = generator.TypeExpression(dataSetType);
var param = generator.ParameterDeclaration("MyParam", paramType);


来源:https://stackoverflow.com/questions/43356807/how-to-create-a-roslyn-itypesymbol-for-an-arbitrary-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!