Single File Generator

This sample demonstrates how to build a single file generator.

2.5 Star
18,262 times
Add to favorites
E-mail Twitter Digg Facebook
Sign in to ask a question

    1 Posts | Last post October 13, 2017
  • Planning any sample upgrades to VS2012?
    2 Posts | Last post December 23, 2013
    • As far as I can see there are only 2 VS2010 SDK samples upgraded to VS2012 in the gallery. I have been unable to upgrade and resolve problems quickly for the VS2010 samples.
      Is there any official plan to release SDK samples for VS2012 soon or do I upgrade and start to upload my 'fixed' samples into the gallery for VS2012?
    • I'd suggest looking at this sample:
      It's a newer sample that's designed to work with newer versions of Visual Studio, and builds a VSIX installer package.
  • Cannot build the C# project, source file CodeGeneratorRegistrationAttribute.cs missing
    7 Posts | Last post October 29, 2013
    • Open and build the solution:
      Error	2	Source file 'C:\Study\SingleFileGen\C#\VisualStudioIntegration\Common\Source\CSharp\RegistrationAttributes\CodeGeneratorRegistrationAttribute.cs' could not be opened ('Unspecified error ')	GeneratorSample
      Looking into the source folders I don't see CodeGeneratorRegistrationAttribute.cs file.
      Did I miss anything? Thanks.
    • This is on the Visual Studio SDK which needs to be downloaded as per the requirements listed in the description of this sample. You can get it at
    • I had the same issue and by looking at your directory path you had the same problem as I did. (Even though I had the SDK installed).
      Unload the project file and edit the location of the the file to where it actually resides, which is not in the project folder but the installation directory of the Sdk. For some reason the path variable used in the project file is not present for my install, so it gives up and seems to assume the project root.
    • Great, just great. I only have VS2013, not VS2010, and the VS2013 SDK doesn't seem to have a CodeGeneratorRegistrationAttribute.cs file! And it seems the VS2010 SDK won't allow me to install it without owning a paid copy of VS2010. What's in this file anyway?
    • Well, I solved the problem of CodeGeneratorRegistrationAttribute.cs. To make it compile in VS2013 I had to eliminate the reference to CodeGeneratorRegistrationAttribute.cs from the csproj file (in Notepad), replace the reference to Microsoft.VisualStudio.Shell.10.0 with Microsoft.VisualStudio.Shell.12.0, and maybe some other things. To install the vsix file after building, I had to add some stuff to the source.extension.vsixmanifest like
            <VisualStudio Version="11.0">
            <VisualStudio Version="12.0">
      But once installed, it just wouldn't work on an XML file. VS lists the extensions under Tools | Extensions and Updates, but when I set Custom Tool to "XmlClassGenerator" and Run Custom Tool, it says "Cannot find custom tool 'XmlClassGenerator' on this system.", and it can't find "C# XML Class Generator" either.
      It looks like for some reason the information about the tool isn't being added to the registry -- the CodeGeneratorRegistration attribute isn't working.
    • Okay, I discovered that if I add [assembly: ComVisible(true)] to AssemblyInfo.cs, the generator sample is installed in the registry. However, it still didn't work!
      At first I got the error "Object reference is not set to an instance of an object". I modified the sample to catch any exceptions in IVsSingleFileGenerator.Generate() and add the stack trace to the message, so that I would find out where in the sample the error occurred. After uninstalling and reinstalling the extension, the error changed to "The custom tool 'XmlClassGenerator' failed. The method or operation is not implemented". Since there was no stack trace, I could tell that the error message did not come from the generator itself.
      I tried to investigate by installing the vsix in the Experimental Instance (to accomplish this, run "Reset the Visual Studio 2013 Experimental Instance" in Start Menu) but the Experimental Instance insisted "Cannot find custom tool 'XmlClassGenerator'". However, soon afterward XmlClassGenerator started "working" in the Visual Studio 2013 main instance (there was still an exception, a security error or something about XSD schemas, I could tell that the sample code was running because the error message included a stack trace!) Then, still later, the exception disappeared and the damn thing started working properly!
      The disturbing thing is, I can't imagine what I did to make it work. All I was doing was trying to debug the problem and it "simply" disappeared. Which means it could just as easily come back later...
      Anyway, this has been about 20 hours I'll never get back. MS, why do you waste our time with samples that don't work and error messages that don't help at all?
    • Okay, finally, when I tried to adapt the sample to implement my own single-file generator, I found that again the generator was not added to the registry during installation of the vsix. There's a file with a ".pkgdef" extension, that is installed with the DLL(s) of the VS extension, this file contains a list of keys that should be installed in the registry. However VS and the vsix installer just ignore it.
      To fix this after installing the vsix, it's necessary to run devenv /setup. It must run in administrator mode or else the error "Operation could not be completed" appears. So I recommend copying the VS shortcut from the start menu to the desktop or something so that you can start as Administrator and add the "/setup" argument to the command line. I'd suggest giving it a few seconds to complete, but no output will appear.
  • How to make this work?
    1 Posts | Last post December 07, 2011
    • What on earth is the Visual Studio Browser referred to in the .doc documentation? I have no idea what that might be and no way to find out if it is a Web site or an application. I can't see anything in VS2010 that looks relevant.
      I have no idea what the VS Experimental hive is either.
      This documentation seems to be written by someone who assumes the reader already knows the obscure technical information he is meant to be communicating.
  • problem with running this sample on VS2010
    2 Posts | Last post December 04, 2011
    • on VS2010 experimental instance it's work after i run the devenv.exe with params:
       /ranu /rootsuffix Exp
      but this sample don't work with the normal instance of VS2010, it's unknown tool for this VS2010.
      i try to register it as it writen on the MSDN link
      replacing "8" with "10".
    • I had to take one more step to make the tool visible, in the properties of the custom tool project, build tab, check the option for Register for COM interop, then re-build.  VS then complained that Microsoft.VisualStudio.Shell.Interop.10.0.dll was not registered, so I had to run regasm on it, and build the tool one more time.
      Also, double check you used the class' CLSID and not the project's guid.
  • Have to use CodeDom
    2 Posts | Last post December 04, 2011
    • Do you have to use CodeDom, or can you simply write out C# as strings?
       populateDictionary.Append("        catch (System.Exception ex)"                                                                                             + Environment.NewLine);
              populateDictionary.Append("        {"                                                                                                                       + Environment.NewLine);
              populateDictionary.Append("            string msg = ex.Message;"                                                                                            + Environment.NewLine);
              populateDictionary.Append("            throw;"                     
    • No, you do not have to use CodeDom.  CodeDom allows you to represent your code in a language independent form, and then generate the code for the language of a selected provider.  
      Personally, I liked the model of the sample, so I used CodeDom minimally, using it to generate the namespace and class, but used CodeSnipitTypeMember for all the class members.  This allowed me to write and format C# code the way I prefer, and take advantage of features in C# that are not available through CodeDom.
  • Needs update for 11 Developer Preview
    1 Posts | Last post September 14, 2011
    • This sample won't build directly in the Developer Preview
  • How is this sample supposed to launch an experimental instance ?
    1 Posts | Last post August 20, 2011
    • Try to download the package (I used C#) and following the step proposed doesn't produce the expected outcome...
      No experimental Hive and no Custom tool available. Bad sample...