views:

27

answers:

1

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!

A: 

Hi Charlie, 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

GarethJ
This does in fact give me a CodeDomProvider. Thank you!
Charlie