XsdDataContractImporter.CodeCompileUnit Property

 

Gets a CodeCompileUnit used for storing the CLR types generated.

Namespace:   System.Runtime.Serialization
Assembly:  System.Runtime.Serialization (in System.Runtime.Serialization.dll)

public CodeCompileUnit CodeCompileUnit { get; }

Property Value

Type: System.CodeDom.CodeCompileUnit

A CodeCompileUnit used to store the CLR types generated.

Call this property after importing schemas using the XsdDataContractImporter to access the CodeCompileUnit in which all the generated code is stored.

The following example uses creates a CodeCompileUnit. The CodeCompileUnit is then used to create both a Visual C# and Visual Basic code file.

static void CompileCode(CodeCompileUnit ccu, string sourceName)
{
    CodeDomProvider provider = null;
    FileInfo sourceFile = new FileInfo(sourceName);
    // Select the code provider based on the input file extension, either C# or Visual Basic.
    if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".CS")
    {
        provider = new Microsoft.CSharp.CSharpCodeProvider();
    }
    else if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".VB")
    {
        provider = new Microsoft.VisualBasic.VBCodeProvider();
    }
    else
    {
        Console.WriteLine("Source file must have a .cs or .vb extension");
    }
    if (provider != null)
    {
        CodeGeneratorOptions options = new CodeGeneratorOptions();
        // Set code formatting options to your preference.
        options.BlankLinesBetweenMembers = true;
        options.BracingStyle = "C";

        StreamWriter sw = new StreamWriter(sourceName);
        provider.GenerateCodeFromCompileUnit(ccu, sw, options);
        sw.Close();
    }
}

.NET Framework
Available since 3.0
Return to top
Show: