XsdDataContractImporter.CodeCompileUnit 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取一个用于存储已生成的 CLR 类型的 CodeCompileUnit。
public:
property System::CodeDom::CodeCompileUnit ^ CodeCompileUnit { System::CodeDom::CodeCompileUnit ^ get(); };
public System.CodeDom.CodeCompileUnit CodeCompileUnit { get; }
member this.CodeCompileUnit : System.CodeDom.CodeCompileUnit
Public ReadOnly Property CodeCompileUnit As CodeCompileUnit
属性值
一个 CodeCompileUnit,用于存储已生成的 CLR 类型。
示例
下面的示例创建了一个 CodeCompileUnit。 然后,使用 CodeCompileUnit 来创建 Visual C# 和 Visual Basic 代码文件。
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();
}
}
Shared Sub CompileCode(ByVal ccu As CodeCompileUnit, ByVal sourceName As String)
Dim provider As CodeDomProvider = Nothing
Dim sourceFile As 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" Then
provider = New Microsoft.CSharp.CSharpCodeProvider()
ElseIf sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) = ".VB" Then
provider = New Microsoft.VisualBasic.VBCodeProvider()
Else
Console.WriteLine("Source file must have a .cs or .vb extension")
End If
If Not (provider Is Nothing) Then
Dim options As New CodeGeneratorOptions()
' Set code formatting options to your preference.
options.BlankLinesBetweenMembers = True
options.BracingStyle = "C"
Dim sw As New StreamWriter(sourceName)
provider.GenerateCodeFromCompileUnit(ccu, sw, options)
sw.Close()
End If
End Sub
注解
在使用 XsdDataContractImporter 导入架构后,调用此属性来访问存储了所有已生成代码的 CodeCompileUnit。