有没有办法访问 Visual Studio 的 CodeSnippet 框架以编程方式操作代码片段?
我正在开发一个将大量模板化的项目,但我正在创建的模板也可能在未来的项目中很好地工作,如 Visual Studio 的 CodeSnippets。
所以,我想知道,有没有一种方法可以访问 Visual Studio 的系统,以任何方式、形状或形式加载、解析和显示 CodeSnippets?
例如,如果能够做这样的事情:
var snippet = LoadSnippet("~/path/to/code.snippet");
if (snippet != null)
{
snippet.TryReplace("$token$", "Some Value");
// and then do something with snippet.ToString(), or...
Console.WriteLine(snippet.GetCSharpString());
}
我知道我可以将 .snippet 文件作为 XML 加载并手动操作它们,但就利用它所做的其他事情而言,例如填写默认令牌值、执行函数来替换令牌,或者控制代码片段文件中标记的可编辑性,如果不需要的话,这比我想要复制的工作要多。
有想法吗?
谢谢!
I'm working on a project that is going to be heavily templated, but the templates I'm creating might also work well in future projects as CodeSnippets for Visual Studio.
So, I was wondering, is there a way to access Visual Studio's system of loading, parsing, and displaying the CodeSnippets in any way, shape, or form?
For example, it would be really slick to be able to do something like this:
var snippet = LoadSnippet("~/path/to/code.snippet");
if (snippet != null)
{
snippet.TryReplace("$token$", "Some Value");
// and then do something with snippet.ToString(), or...
Console.WriteLine(snippet.GetCSharpString());
}
I know I could just load the .snippet files as XML and manipulate them manually, but in terms of taking advantage of the other things it does such as fill in default token values, execute functions to replace tokens, or control the editability of the tokens in the snippet file, that's more work than I want to duplicate if I don't have to.
Ideas?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该考虑使用“真正的”模板工具,例如 CodeSmith 或 Visual Studio 2010 的新功能。这些模板在执行期间支持“代码隐藏”。不确定 Visual Studio 工具集,但 CodeSmith 可以通过 API 执行。
片段要简单得多。不要试图让他们成为他们不是的人。如果您想使用片段,可以免费下载一些工具(片段编辑器,http://www.codeplex.com /SnippetEditor) 来帮助更好地设计它们、利用这些功能等,这样您就不必过多地使用原始 XML。
You should consider a "real" templating tool such as CodeSmith, or the new feature of Visual Studio 2010. These templates support "code-behind" during execution. Not sure of the Visual Studio toolset, but CodeSmith can be executed via API.
Snippets are much much much simpler. Don't try to make them something they are not. If you wish to use snippets there are some free-download tools (Snippet Editor, http://www.codeplex.com/SnippetEditor) to help better design them, take advantage of the features, etc, so you aren't in the raw XML so much.