Click here to Skip to main content
15,894,180 members
Articles / Programming Languages / Visual Basic

Source code generator for any data type

Rate me:
Please Sign up or sign in to vote.
4.59/5 (19 votes)
5 May 2008CPOL5 min read 57.4K   919   63  
How to extend Visual Studio so it can generate code for any data type.
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;

namespace Xsd2Class
{
    [ComVisible(true)]
    [Guid("0e439c89-b1be-489d-a0f4-c2d191db6f9b")]
    public class Xsd2Class : Microsoft.CustomTool.BaseCodeGeneratorWithSite
    {
        public string GetLanguage()
        {
            string DefaultExtension = GetDefaultExtension();
            switch (DefaultExtension)
            {
                case ".jsl":
                    return "vjs";
                default:
                    return DefaultExtension;
            }
        }

        public override string GetDefaultExtension()
        {
            return base.GetDefaultExtension();
        }

        protected override byte[] GenerateCode(string inputFileName, string inputFileContent)
        {
            GenerateCodeFile(inputFileName);
            byte[] GeneratedFileContent = GetGeneratedFileContent(inputFileName);
            return GeneratedFileContent;
        }

        private byte[] GetGeneratedFileContent(string inputFileName)
        {
            string DefaultExtension = GetDefaultExtension();
            string outputFilename = inputFileName.Replace(".xsd", DefaultExtension);
            FileInfo FI = new FileInfo(outputFilename);
            using (StreamReader FS = FI.OpenText())
            {
                string Contents = FS.ReadToEnd();
                return System.Text.Encoding.ASCII.GetBytes(Contents);
            }
        }

        private void GenerateCodeFile(string inputFileName)
        {
            FileInfo FI = new FileInfo(inputFileName);

            Process proc = new Process();
            proc.StartInfo.FileName = @"C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\xsd.exe";
            string Namespace = FI.Name.Substring(0, FI.Name.Length - FI.Extension.Length);
            string Language = GetLanguage();
            proc.StartInfo.Arguments =
                        String.Format(@"/c /l:{0} /n:{1} ""{2}"" /out:""{3}""", Language, Namespace, inputFileName, FI.DirectoryName);
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.CreateNoWindow = true;
            proc.StartInfo.RedirectStandardError = true;
            proc.Start();
            proc.WaitForExit();
            if (proc.ExitCode != 0)
            {
                using (StreamReader SR = proc.StandardError)
                {
                    string Errors = SR.ReadToEnd();
                    if (!(string.IsNullOrEmpty(Errors)))
                    {
                        throw new Exception(Errors);
                    }
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer ViCi Consulting
Netherlands Netherlands
Though I graduated as an Electro Technical Engineer, I specialized myself in writing software. At this moment I work as a consultant for many clients. I get to work with a lot of different technologies: VBA, WinForms, WebForms, ADO.NET, MSAccess, eXtreme Programming and a lot more.

Comments and Discussions