本文整理汇总了C#中System.CodeDom.CodeCompileUnit.AddNamespace方法的典型用法代码示例。如果您正苦于以下问题:C# CodeCompileUnit.AddNamespace方法的具体用法?C# CodeCompileUnit.AddNamespace怎么用?C# CodeCompileUnit.AddNamespace使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.CodeDom.CodeCompileUnit
的用法示例。
在下文中一共展示了CodeCompileUnit.AddNamespace方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenerateEntityClasses
/// <summary>
/// Generates POCO entities based on the supplied model
/// </summary>
/// <param name="model">Model to generate classes from</param>
/// <returns>Generated code files</returns>
public IEnumerable<FileContents<string>> GenerateEntityClasses(EntityModelSchema model)
{
ExceptionUtilities.CheckArgumentNotNull(model, "model");
PocoAnnotator.Annotate(model, this.PocoOption);
List<FileContents<string>> results = new List<FileContents<string>>();
foreach (var ns in model.EntityTypes.Select(e => e.NamespaceName).Concat(model.EnumTypes.Select(e => e.NamespaceName)).Concat(model.ComplexTypes.Select(e => e.NamespaceName)).Distinct())
{
var codeUnit = new CodeCompileUnit();
CodeNamespace codeNamespace = codeUnit.AddNamespace(ns);
codeNamespace.ImportNamespace("System.Collections.Generic");
foreach (var type in model.ComplexTypes.Where(e => e.NamespaceName == ns))
{
codeNamespace.Types.Add(this.BuildType(type));
}
foreach (var type in model.EntityTypes.Where(e => e.NamespaceName == ns))
{
codeNamespace.Types.Add(this.BuildType(type));
}
foreach (var type in model.EnumTypes.Where(e => e.NamespaceName == ns))
{
codeNamespace.Types.Add(this.BuildType(type));
}
string code = this.GenerateCodeFromCompileUnit(codeUnit);
results.Add(new FileContents<string>(ns + this.language.FileExtension, code));
}
return results;
}
示例2: GenerateCode
public void GenerateCode(FileProjectItem item, CustomToolContext context)
{
XmlDocument doc = new XmlDocument();
doc.Load(item.FileName);
SettingsDocument setDoc = new SettingsDocument(doc.DocumentElement, DummySettingsEntryHost.Instance);
string customToolNamespace = item.GetEvaluatedMetadata("CustomToolNamespace");
if (!string.IsNullOrEmpty(customToolNamespace)) {
setDoc.GeneratedClassNamespace = customToolNamespace;
}
CodeCompileUnit ccu = new CodeCompileUnit();
ccu.AddNamespace(setDoc.GeneratedClassNamespace).Types.Add(CreateClass(setDoc));
context.WriteCodeDomToFile(item, context.GetOutputFileName(item, ".Designer"), ccu);
}
示例3: Main
public static void Main(string[] args)
{
string directory = "../../../ICSharpCode.NRefactory.VB/Ast/";
string visitorsDir = "../../../ICSharpCode.NRefactory.VB/Visitors/";
Debug.WriteLine("AST Generator running...");
if (!File.Exists(directory + "INode.cs")) {
Debug.WriteLine("did not find output directory");
return;
}
if (!File.Exists(visitorsDir + "AbstractAstTransformer.cs")) {
Debug.WriteLine("did not find visitor output directory");
return;
}
List<Type> nodeTypes = new List<Type>();
foreach (Type type in typeof(MainClass).Assembly.GetTypes()) {
if (type.IsClass && typeof(INode).IsAssignableFrom(type)) {
nodeTypes.Add(type);
}
}
nodeTypes.Sort(delegate(Type a, Type b) { return a.Name.CompareTo(b.Name); });
CodeCompileUnit ccu = new CodeCompileUnit();
CodeNamespace cns = ccu.AddNamespace("ICSharpCode.NRefactory.VB.Ast");
cns.AddImport("System");
cns.AddImport("System.Collections.Generic");
foreach (Type type in nodeTypes) {
if (type.GetCustomAttributes(typeof(CustomImplementationAttribute), false).Length == 0) {
CodeTypeDeclaration ctd = cns.AddType(type.Name);
if (type.IsAbstract) {
ctd.TypeAttributes |= TypeAttributes.Abstract;
}
ctd.BaseTypes.Add(new CodeTypeReference(type.BaseType.Name));
ProcessType(type, ctd);
foreach (object o in type.GetCustomAttributes(false)) {
if (o is TypeImplementationModifierAttribute) {
(o as TypeImplementationModifierAttribute).ModifyImplementation(cns, ctd, type);
}
}
if (!type.IsAbstract) {
CodeMemberMethod method = new CodeMemberMethod();
method.Name = "AcceptVisitor";
method.Attributes = MemberAttributes.Public | MemberAttributes.Override;
method.Parameters.Add(new CodeParameterDeclarationExpression("IAstVisitor", "visitor"));
method.Parameters.Add(new CodeParameterDeclarationExpression(typeof(object), "data"));
method.ReturnType = new CodeTypeReference(typeof(object));
CodeExpression ex = new CodeVariableReferenceExpression("visitor");
ex = new CodeMethodInvokeExpression(ex, VisitPrefix + ctd.Name,
new CodeThisReferenceExpression(),
new CodeVariableReferenceExpression("data"));
method.Statements.Add(new CodeMethodReturnStatement(ex));
ctd.Members.Add(method);
method = new CodeMemberMethod();
method.Name = "ToString";
method.Attributes = MemberAttributes.Public | MemberAttributes.Override;
method.ReturnType = new CodeTypeReference(typeof(string));
method.Statements.Add(new CodeMethodReturnStatement(CreateToString(type)));
ctd.Members.Add(method);
}
}
}
System.CodeDom.Compiler.CodeGeneratorOptions settings = new System.CodeDom.Compiler.CodeGeneratorOptions();
settings.IndentString = "\t";
settings.VerbatimOrder = true;
using (StringWriter writer = new StringWriter()) {
new Microsoft.CSharp.CSharpCodeProvider().GenerateCodeFromCompileUnit(ccu, writer, settings);
File.WriteAllText(directory + "Generated.cs", NormalizeNewLines(writer));
}
ccu = new CodeCompileUnit();
cns = ccu.AddNamespace("ICSharpCode.NRefactory.VB");
cns.AddImport("System");
cns.AddImport("ICSharpCode.NRefactory.VB.Ast");
cns.Types.Add(CreateAstVisitorInterface(nodeTypes));
using (StringWriter writer = new StringWriter()) {
new Microsoft.CSharp.CSharpCodeProvider().GenerateCodeFromCompileUnit(ccu, writer, settings);
File.WriteAllText(visitorsDir + "../IAstVisitor.cs", NormalizeNewLines(writer));
}
ccu = new CodeCompileUnit();
cns = ccu.AddNamespace("ICSharpCode.NRefactory.VB.Visitors");
cns.AddImport("System");
cns.AddImport("System.Collections.Generic");
cns.AddImport("System.Diagnostics");
cns.AddImport("ICSharpCode.NRefactory.VB.Ast");
cns.Types.Add(CreateAstVisitorClass(nodeTypes, false));
using (StringWriter writer = new StringWriter()) {
new Microsoft.CSharp.CSharpCodeProvider().GenerateCodeFromCompileUnit(ccu, writer, settings);
File.WriteAllText(visitorsDir + "AbstractAstVisitor.cs", NormalizeNewLines(writer));
}
//.........这里部分代码省略.........
示例4: GenerateClientCode
/// <summary>
/// Generates the client-side proxy classes then calls the given callback
/// </summary>
/// <param name="continuation">The async continuation to report completion on</param>
/// <param name="serviceRoot">The root uri of the service</param>
/// <param name="model">The model for the service</param>
/// <param name="language">The language to generate code in</param>
/// <param name="onCompletion">The action to invoke with the generated code</param>
public void GenerateClientCode(IAsyncContinuation continuation, Uri serviceRoot, EntityModelSchema model, IProgrammingLanguageStrategy language, Action<string> onCompletion)
{
ExceptionUtilities.CheckArgumentNotNull(continuation, "continuation");
ExceptionUtilities.CheckArgumentNotNull(serviceRoot, "serviceRoot");
ExceptionUtilities.CheckArgumentNotNull(model, "model");
ExceptionUtilities.CheckArgumentNotNull(language, "language");
ExceptionUtilities.CheckArgumentNotNull(onCompletion, "onCompletion");
ExceptionUtilities.CheckAllRequiredDependencies(this);
// because the product code-gen does not produce this overload of the DataServiceContext constructor, we need to add it ourselves
// namespace <contextNamespace>
// {
// partial class <contextType>
// {
// public <contextType>(Uri serviceUri, DataServiceProtocolVersion maxProtocolVersion)
// : base(serviceUri, maxProtocolVersion)
// {
// }
// }
// }
var compileUnit = new CodeCompileUnit();
var contextNamespace = compileUnit.AddNamespace(model.EntityTypes.First().NamespaceName);
var contextType = contextNamespace.DeclareType(model.EntityContainers.Single().Name);
contextType.IsPartial = true;
contextType.AddConstructor()
.WithArgument(Code.TypeRef<Uri>(), "serviceUri")
.WithArgument(Code.TypeRef("Microsoft.OData.Client.ODataProtocolVersion"), "maxProtocolVersion")
.WithBaseConstructorArgument(Code.Variable("serviceUri"))
.WithBaseConstructorArgument(Code.Variable("maxProtocolVersion"));
string constructorOverload = language.CreateCodeGenerator().GenerateCodeFromNamespace(contextNamespace);
#if !WIN8
this.DataServiceBuilder.BeginGenerateClientLayerCode(
serviceRoot.OriginalString,
this.DesignVersion,
this.ClientVersion,
language.FileExtension,
result =>
{
AsyncHelpers.CatchErrors(
continuation,
() =>
{
string errorMessage;
string clientCode = this.DataServiceBuilder.EndGenerateClientLayerCode(out errorMessage, result);
if (errorMessage != null)
{
throw new TaupoInfrastructureException(errorMessage);
}
// add the extra constructor overload we generated above
clientCode = string.Concat(clientCode, Environment.NewLine, constructorOverload);
onCompletion(clientCode);
continuation.Continue();
});
},
null);
#else
var task = this.DataServiceBuilder.GenerateClientLayerCodeAsync(
new GenerateClientLayerCodeRequest(
serviceRoot.OriginalString,
this.DesignVersion,
this.ClientVersion,
language.FileExtension));
task.Wait();
var result = task.Result;
string clientCode = result.GenerateClientLayerCodeResult;
string errorMessage = result.errorLog;
if (errorMessage != null)
{
throw new TaupoInfrastructureException(errorMessage);
}
// add the extra constructor overload we generated above
clientCode = string.Concat(clientCode, Environment.NewLine, constructorOverload);
onCompletion(clientCode);
continuation.Continue();
#endif
}
示例5: BuildCompiledCodeUnit
/// <summary>
/// Builds a <see cref="CodeCompileUnit"/> containing all the compiled variations
/// </summary>
/// <returns>Code unit containing a single class (Tests.TestClass) with a method for each variation</returns>
private CodeCompileUnit BuildCompiledCodeUnit()
{
var ccu = new CodeCompileUnit();
var ns = ccu.AddNamespace("Tests");
foreach (var import in this.namespaceImports)
{
ns.ImportNamespace(import);
}
var codeClass = ns.DeclareType("TestClass").InheritsFrom(Code.TypeRef<GeneratedCode>());
foreach (var externalProperty in this.externalProperties.Values)
{
codeClass.AddAutoImplementedProperty(externalProperty.PropertyType, externalProperty.PropertyName);
}
// Add variations to class
foreach (var v in this.variations)
{
CodeMemberMethod codeVariation = codeClass.AddMethod(v.BlockName);
if (v.IsAsynchronous)
{
codeVariation = codeVariation.WithArgument(Code.TypeRef<IAsyncContinuation>(), "continuation");
}
codeVariation.Statements.AddRange(v.Statements);
}
codeClass.Members.AddRange(this.customMembers.ToArray());
return ccu;
}
示例6: Main
public static void Main(string[] args)
{
string directory = "../../NRefactory/Project/Src/Ast/";
string visitorsDir = "../../NRefactory/Project/Src/Visitors/";
Console.WriteLine("AST Generator running...");
if (!File.Exists(directory + "INode.cs")) {
Console.WriteLine("did not find output directory " + Path.GetFullPath(Path.GetDirectoryName(directory)));
return;
}
if (!File.Exists(visitorsDir + "AbstractAstTransformer.cs")) {
Console.WriteLine("did not find visitor output directory");
return;
}
List<Type> nodeTypes = new List<Type>();
foreach (Type type in typeof(MainClass).Assembly.GetTypes()) {
if (type.IsClass && typeof(INode).IsAssignableFrom(type)) {
nodeTypes.Add(type);
}
}
nodeTypes.Sort(delegate(Type a, Type b) { return a.Name.CompareTo(b.Name); });
CodeCompileUnit ccu = new CodeCompileUnit();
CodeNamespace cns = ccu.AddNamespace("ICSharpCode.NRefactory.Ast");
cns.AddImport("System");
cns.AddImport("System.Collections.Generic");
cns.AddImport("AgentRalph.Visitors");
foreach (Type type in nodeTypes) {
if (type.GetCustomAttributes(typeof(CustomImplementationAttribute), false).Length == 0) {
CodeTypeDeclaration ctd = cns.AddType(type.Name);
if (type.IsAbstract) {
ctd.TypeAttributes |= TypeAttributes.Abstract;
}
ctd.IsPartial = true;
ctd.BaseTypes.Add(new CodeTypeReference(type.BaseType.Name));
ProcessType(type, ctd);
foreach (object o in type.GetCustomAttributes(false)) {
if (o is TypeImplementationModifierAttribute) {
(o as TypeImplementationModifierAttribute).ModifyImplementation(cns, ctd, type);
}
}
if (!type.IsAbstract) {
AddAnAcceptVisitorMethod(type, ctd, "IAstVisitor", typeof(object));
// Create an overload
AddAnAcceptVisitorMethod(type, ctd, "AstComparisonVisitor", typeof(bool));
AddAChillunsGetter(type, ctd);
var method = new CodeMemberMethod();
method.Name = "ToString";
method.Attributes = MemberAttributes.Public | MemberAttributes.Override;
method.ReturnType = new CodeTypeReference(typeof(string));
method.Statements.Add(new CodeMethodReturnStatement(CreateToString(type)));
ctd.Members.Add(method);
}
}
}
System.CodeDom.Compiler.CodeGeneratorOptions settings = new System.CodeDom.Compiler.CodeGeneratorOptions();
settings.IndentString = "\t";
settings.VerbatimOrder = true;
using (StringWriter writer = new StringWriter()) {
new Microsoft.CSharp.CSharpCodeProvider().GenerateCodeFromCompileUnit(ccu, writer, settings);
File.WriteAllText(directory + "Generated.cs", writer.ToString());
}
ccu = new CodeCompileUnit();
cns = ccu.AddNamespace("ICSharpCode.NRefactory");
cns.AddImport("System");
cns.AddImport("ICSharpCode.NRefactory.Ast");
cns.Types.Add(CreateAstVisitorInterface(nodeTypes));
using (StringWriter writer = new StringWriter()) {
new Microsoft.CSharp.CSharpCodeProvider().GenerateCodeFromCompileUnit(ccu, writer, settings);
File.WriteAllText(visitorsDir + "../IAstVisitor.cs", writer.ToString());
}
// My modifications to the generation process are not preserving the creation of these particular files.
// So I just don't regenerate them for the time being.
// ccu = new CodeCompileUnit();
// cns = ccu.AddNamespace("ICSharpCode.NRefactory.Visitors");
// cns.AddImport("System");
// cns.AddImport("System.Collections.Generic");
// cns.AddImport("System.Diagnostics");
// cns.AddImport("ICSharpCode.NRefactory.Ast");
// cns.Types.Add(CreateAstVisitorClass(nodeTypes, false));
//
// using (StringWriter writer = new StringWriter()) {
// new Microsoft.CSharp.CSharpCodeProvider().GenerateCodeFromCompileUnit(ccu, writer, settings);
// File.WriteAllText(visitorsDir + "AbstractAstVisitor.cs", writer.ToString());
// }
//
// ccu = new CodeCompileUnit();
// cns = ccu.AddNamespace("ICSharpCode.NRefactory.Visitors");
// cns.AddImport("System");
//.........这里部分代码省略.........