如何从继承自 Microsoft.VisualStudio.TextTemplated.VSHost.BaseCodeGeneratorWithSite 的类访问 CodeDomProvider?

发布于 2024-09-11 15:55:18 字数 2496 浏览 3 评论 0原文

有谁知道如何从 Visual Studio 2010 SDK 中的新 Microsoft.VisualStudio.TextTemplated.VSHost.BaseCodeGeneratorWithSite 中获取 CodeDomProvider ?我过去只是通过继承 Microsoft.CustomTool.BaseCodeGeneratorWithSite 类来访问它,但现在有了这个新类,它就不存在了。我看到 GlobalServiceProvider 和 SiteServiceProvider 但我找不到任何有关如何使用它们的示例。

Microsoft.VisualStudio.TextTemplate.VSHost.BaseCodeGeneratorWithSite: http://msdn.microsoft.com/en-us/library/bb932625。 aspx

我是这样做的:

public class Generator : Microsoft.VisualStudio.TextTemplating.VSHost.BaseCodeGeneratorWithSite {

    public override string GetDefaultExtension() {
        // GetDefaultExtension IS ALSO NOT ACCESSIBLE...
        return this.InputFilePath.Substring(this.InputFilePath.LastIndexOf(".")) + ".designer" + base.GetDefaultExtension();
    }

    // This method is being called every time the attached xml is saved.
    protected override byte[] GenerateCode(string inputFileName, string inputFileContent) {
        try {
            // Try to generate the wrapper file.
            return GenerateSourceCode(inputFileName);
        } catch (Exception ex) {
            // In case of a faliure - print the exception 
            // as a comment in the source code.
            return GenerateExceptionCode(ex);
        }
    }

    public byte[] GenerateSourceCode(string inputFileName) {
        Dictionary<string, CodeCompileUnit> oCodeUnits;

        // THIS IS WHERE CodeProvider IS NOT ACCESSIBLE
        CodeDomProvider oCodeDomProvider = this.CodeProvider;

        string[] aCode = new MyCustomAPI.GenerateCode(inputFileName, ref oCodeDomProvider);
        return Encoding.ASCII.GetBytes(String.Join(@"
", aCode));
    }

    private byte[] GenerateExceptionCode(Exception ex) {
        CodeCompileUnit oCode = new CodeCompileUnit();

        CodeNamespace oNamespace = new CodeNamespace("System");
        oNamespace.Comments.Add(new CodeCommentStatement(MyCustomAPI.Print(ex)));
        oCode.Namespaces.Add(oNamespace);
        string sCode = null;
        using (StringWriter oSW = new StringWriter()) {
            using (IndentedTextWriter oITW = new IndentedTextWriter(oSW)) {
                this.CodeProvider.GenerateCodeFromCompileUnit(oCode, oITW, null);
                sCode = oSW.ToString();
            }
        }
        return Encoding.ASCII.GetBytes(sCode );
    }
}

感谢您的帮助!

Does anyone know how to get a CodeDomProvider in the new Microsoft.VisualStudio.TextTemplating.VSHost.BaseCodeGeneratorWithSite from the Visual Studio 2010 SDK? I used to get access to it just by in mere inheritance of the class Microsoft.CustomTool.BaseCodeGeneratorWithSite, but now with this new class it is not there. I see a GlobalServiceProvider and a SiteServiceProvider but I can't find any example on how to use them.

Microsoft.VisualStudio.TextTemplating.VSHost.BaseCodeGeneratorWithSite:
http://msdn.microsoft.com/en-us/library/bb932625.aspx

I was to do this:

public class Generator : Microsoft.VisualStudio.TextTemplating.VSHost.BaseCodeGeneratorWithSite {

    public override string GetDefaultExtension() {
        // GetDefaultExtension IS ALSO NOT ACCESSIBLE...
        return this.InputFilePath.Substring(this.InputFilePath.LastIndexOf(".")) + ".designer" + base.GetDefaultExtension();
    }

    // This method is being called every time the attached xml is saved.
    protected override byte[] GenerateCode(string inputFileName, string inputFileContent) {
        try {
            // Try to generate the wrapper file.
            return GenerateSourceCode(inputFileName);
        } catch (Exception ex) {
            // In case of a faliure - print the exception 
            // as a comment in the source code.
            return GenerateExceptionCode(ex);
        }
    }

    public byte[] GenerateSourceCode(string inputFileName) {
        Dictionary<string, CodeCompileUnit> oCodeUnits;

        // THIS IS WHERE CodeProvider IS NOT ACCESSIBLE
        CodeDomProvider oCodeDomProvider = this.CodeProvider;

        string[] aCode = new MyCustomAPI.GenerateCode(inputFileName, ref oCodeDomProvider);
        return Encoding.ASCII.GetBytes(String.Join(@"
", aCode));
    }

    private byte[] GenerateExceptionCode(Exception ex) {
        CodeCompileUnit oCode = new CodeCompileUnit();

        CodeNamespace oNamespace = new CodeNamespace("System");
        oNamespace.Comments.Add(new CodeCommentStatement(MyCustomAPI.Print(ex)));
        oCode.Namespaces.Add(oNamespace);
        string sCode = null;
        using (StringWriter oSW = new StringWriter()) {
            using (IndentedTextWriter oITW = new IndentedTextWriter(oSW)) {
                this.CodeProvider.GenerateCodeFromCompileUnit(oCode, oITW, null);
                sCode = oSW.ToString();
            }
        }
        return Encoding.ASCII.GetBytes(sCode );
    }
}

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

留一抹残留的笑 2024-09-18 15:55:18

您可以通过请求 SVSMDCodeDomProvider 服务,通过 SiteServiceProvider 访问 CodeDomProvider。

大致意思是:

IVSMDCodeDomProvider provider = SiteServiceProvider. 
        GetService(typeof(SVSMDCodeDomProvider)) as IVSMDCodeDomProvider;

if (provider != null)
{
    codeDomProvider = provider.CodeDomProvider as CodeDomProvider;
}

SiteServiceProvider 是 SingleFileGenerator 站点公开的有限范围服务提供者,而 GlobalServiceProvider 是 VS 的主要服务提供者,您可以要求任何全局范围的接口。

希望这有帮助。
加雷思

You can access the CodeDomProvider via the SiteServiceProvider by asking for the SVSMDCodeDomProvider service.

Something along the lines of:

IVSMDCodeDomProvider provider = SiteServiceProvider. 
        GetService(typeof(SVSMDCodeDomProvider)) as IVSMDCodeDomProvider;

if (provider != null)
{
    codeDomProvider = provider.CodeDomProvider as CodeDomProvider;
}

The SiteServiceProvider is the limited scope service provider exposed by the site of a SingleFileGenerator, whereas the GlobalServiceProvider is VS' main service provider that you can ask for any globally-scoped interface.

Hope this helps.
Gareth

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文