在CodeSnippetCompileUnit中导入名称空间 [英] Import namespaces in a CodeSnippetCompileUnit

查看:52
本文介绍了在CodeSnippetCompileUnit中导入名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用CodeCompileUnit通过CodeDOM生成代码时,可以通过创建与定义类型的名称空间分开的CodeNamespace来导入名称空间.使用Imports属性,可以添加所需的名称空间.如果将CodeNamespace添加到CodeCompile单元,则导入将显示在CodeDOM生成的文件的顶部.

When using a CodeCompileUnit to generate code via the CodeDOM, you can import namespaces by creating a CodeNamespace separate from the namespace in which you're defining your type(s). Using the Imports property allows you to add the namespaces you desire. If you add the CodeNamespace to the CodeCompile unit, the imports will appear at the top of the file generated by the CodeDOM.

例如,使用CSharpCodeProvider编译以下CodeDOM图:

For instance, using a CSharpCodeProvider to compile the following CodeDOM graph:

CodeCompileUnit compileUnit = new CodeCompileUnit();
CodeNamespace importsNamespace = new CodeNamespace {Imports = {new CodeNamespaceImport("System"), new CodeNamespaceImport("System.Collections.Generic"), new CodeNamespaceImport("System.Linq")}};
CodeNamespace typeNamespace = new CodeNamespace("MyTypeNamespace");
compileUnit.Namespaces.Add(importsNamespace);
compileUnit.Namespaces.Add(typeNamespace);

将生成

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.269
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;

namespace MyTypeNamespace {

}

CodeSnippetCompileUnit允许您编译已作为字符串输入的类型定义.例如,字符串公共类TestClass {}"可以用作CodeSnippetCompileUnit的值.对CodeSnippetCompileUnit使用与上述相同的代码:

The CodeSnippetCompileUnit allows you to compile type definitions which have been entered as a string. For instance, the string "public class TestClass {}" can be used as the Value for a CodeSnippetCompileUnit. Using the same code as above with a CodeSnippetCompileUnit:

const string testClassCodeString = "public class TestClass {}";
CodeSnippetCompileUnit snippetCompileUnit = new CodeSnippetCompileUnit {Value = testClassCodeString};
CodeNamespace importsNamespace = new CodeNamespace {Imports = {new CodeNamespaceImport("System"), new CodeNamespaceImport("System.Collections.Generic"), new CodeNamespaceImport("System.Linq")}};
CodeNamespace typeNamespace = new CodeNamespace("MyTypeNamespace");
compileUnit.Namespaces.Add(importsNamespace);
compileUnit.Namespaces.Add(typeNamespace);

将生成一个仅包含以下内容的文件:

will generate a file which contains only:

public class TestClass {}

如何将这种类型包含在命名空间中,或将命名空间导入到从CodeSnippetCompileUnit生成的文件中?

How can you enclose this type in a namespace, or import namespaces to the file generated from a CodeSnippetCompileUnit?

推荐答案

即使CodeSnippetCompileUnit是从CodeCompileUnit派生的,编译器在从CodeDOM图生成代码时也会忽略Namespaces属性.从CodeSnippetCompileUnit生成的代码仅包含对象的Value属性中的字符串.

Even though CodeSnippetCompileUnit is derived from CodeCompileUnit, the compiler ignores the Namespaces property when generating code from the CodeDOM graph. Code generated from the CodeSnippetCompileUnit contains only the string that is in the Value property of the object.

如果您具有上下文无关的代码段,则导入命名空间或将代码段包含在命名空间中的唯一选择是在设置CodeSnippetCompileUnit的值之前修改字符串.

If you have a context-free code snippet, your only option to import namespaces or enclose the snippet in a namespace is to modify the string before setting the Value of the CodeSnippetCompileUnit.

使用上述问题中的代码作为起点的示例:

An example using the code in the question above as a starting place:

string namespaceString = "MyTypeNamespace";
string codeString = "public class TestClass {}";
string snippetValue = string.Format(@"
using System;
using System.Collections.Generic;
using System.Linq;

namespace {0}
{{

{1}

}}", namespaceString, codeString, Environment.NewLine);

var snippetCompileUnit = new CodeSnippetCompileUnit {Value = snippetValue};

这将生成:

using System;
using System.Collections.Generic;
using System.Linq;

namespace MyTypeNamespace
{

public class TestClass {}

}

这篇关于在CodeSnippetCompileUnit中导入名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆