0

I've been trying to create some snippets and have not been able to find a way to automatically import NuGet packages when the user prompts the snippets.

The documentation includes instructions on automatically including locally available resources (such as System.Collections.Generic), but not externally available resources (such as System.Data.SqlClient)

Any help on the matter would be greatly appreciated!

Sebastian Inones
  • 1,561
  • 1
  • 19
  • 32
  • Hi Anders, if check if my answer helps you handle this issue. And if the answer help you handle and understand this issue, you could [consider accepting the answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) so that it will help other community members search and handle similar issues. And if not, please feel free to let us know. It is kind of you to do that and it will help us create a good community environment. – Mr Qian Jun 03 '20 at 03:31

2 Answers2

1

i do not think that this is possible.

if your snippet is able to update the project file then you could do a Restore NuGet Packages

Daniel
  • 9,491
  • 12
  • 50
  • 66
  • Thanks for the answer - think you are rightfully assuming that it can't be done. The purpose of the snippets was to easily setup new similar projects. Would a Visual Studio project template including the custom snippets be the way to go, or is there some obvious solution I'm overlooking? – Anders Hoffmann May 11 '20 at 06:24
  • You are not overlooking something. There are solutions on the market that allow sharing of snippets throughout your team (which is what i believe you are looking for). You have to fix the import of the references manually. – Daniel May 11 '20 at 06:27
0

Is there a way to automatically import necessary NuGet packages, when using a snippet?

I think you cannot get what you want.

Code snippet is equivalent to quickly inserting code snippets, only acting on the Code Editor, so it cannot exceed the scope of the editor itself, install the nuget package in the nuget package management UI, and reference it.

The import node in Code Snippet is just custom by ourselves. VS does not have the job to detect it and correct it.

Therefore, if the package itself is already referenced when inserting the fragment, no error will be reported. If there is no reference, inserting the segment will cause an error, and you will need to import the package manually, not install it automatically.

Code Snippet does not have the task to install nuget packages at the same time.

In summary, its role is simply to quickly output your own code blocks to the Code Editor. And if you still want it, I think creating such VS Extension can realize it, but it can be a bit complex.

As a suggestion, you can try to create a project template with the required nuget packages which already contains the code snippets. See this link.

Mr Qian
  • 21,064
  • 1
  • 31
  • 41