如何从Roslyn中的符号信息构建通用类型

huangapple go评论60阅读模式
英文:

How to construct generic type from symbol info in roslyn

问题

INamedTypeSymbol - System.Action<MyAwsomeApp.Foo>.

英文:

I have INamedTypeSymbol, lets say MyAwsomeApp.Foo. I would like to wrap it in Action<>. How I can achieve this?

Expected output is INamedTypeSymbol - System.Action<MyAwseomeApp.Foo>.

答案1

得分: 1

你可以使用 INamedType.Construct 方法来实现这个,例如:

var actionOfT = compilation.GetTypeByMetadataName(typeof(Action<>).FullName);
var foo = compilation.GetTypeByMetadataName("MyAwseomeApp.Foo");
var actionOfFoo = actionOfT.Construct(foo);
英文:

You can use the INamedType.Construct method for that, e.g.

var actionOfT = compilation.GetTypeByMetadataName(typeof(Action<>).FullName);
var foo = compilation.GetTypeByMetadataName("MyAwseomeApp.Foo");
var actionOfFoo = actionOfT.Construct(foo);

huangapple
  • 本文由 发表于 2023年4月4日 06:21:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924156.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定