How to place IDeclaredType name in expression?

Hello again,

my second issue is how to add create "new List<Foo>()" ICSharpExpression, if we have IDeclaredType representing "List<Foo>"? My solution is:

 string typePresentableName = declaredType.GetPresentableName(CSharpLanguage.Instance);

 var factory = CSharpElementFactory.GetInstance(Provider.PsiModule);

 string code = $"new {typePresentableName}()";

 ICSharpExpression newExpression = factory.CreateExpression(code);

It works and it's again string-based logic. Can this code be improved?

ReSharper SDK 9.2 is used.

1 comment
Comment actions Permalink
Official comment

You can use factory.CreateExpression("new {0}(code)", declaredType). You can pass multiple things as parameters for CreateExpression, such as string, declared elements, ICSharpStatement, IType, etc.

Please sign in to leave a comment.