PageRenderTime 45ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/SolutionFramework/Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.10.0/Microsoft/VisualStudio/ServiceModel/DomainServices/Tools/CodeGenContext.cs

#
C# | 198 lines | 179 code | 19 blank | 0 comment | 15 complexity | b3c453f35ac4b9f6e448e2306640a01b MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0
  1. namespace Microsoft.VisualStudio.ServiceModel.DomainServices.Tools
  2. {
  3. using Microsoft.CSharp;
  4. using Microsoft.VisualBasic;
  5. using System;
  6. using System.CodeDom;
  7. using System.CodeDom.Compiler;
  8. using System.Collections.Generic;
  9. using System.Globalization;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. internal sealed class CodeGenContext : IDisposable
  14. {
  15. private CodeCompileUnit _compileUnit = new CodeCompileUnit();
  16. private Dictionary<string, CodeNamespace> _namespaces = new Dictionary<string, CodeNamespace>();
  17. private System.CodeDom.Compiler.CodeGeneratorOptions _options = new System.CodeDom.Compiler.CodeGeneratorOptions();
  18. private CodeDomProvider _provider;
  19. private List<string> _references = new List<string>();
  20. private string _rootNamespace;
  21. public CodeGenContext(string language, string rootNamespace)
  22. {
  23. this._provider = language.Equals("C#") ? ((CodeDomProvider) new CSharpCodeProvider()) : ((CodeDomProvider) new VBCodeProvider());
  24. this._rootNamespace = rootNamespace;
  25. this.Initialize();
  26. }
  27. internal void AddReference(string reference)
  28. {
  29. if (!this._references.Contains<string>(reference, StringComparer.OrdinalIgnoreCase))
  30. {
  31. this._references.Add(reference);
  32. }
  33. }
  34. public void Dispose()
  35. {
  36. GC.SuppressFinalize(this);
  37. CodeDomProvider provider = this._provider;
  38. this._provider = null;
  39. if (provider != null)
  40. {
  41. provider.Dispose();
  42. }
  43. this._compileUnit = null;
  44. this._namespaces = null;
  45. }
  46. private void FixUpCompileUnit(CodeCompileUnit compileUnit)
  47. {
  48. new DomainServiceFixupCodeDomVisitor(this).Visit(compileUnit);
  49. }
  50. private string FixupVBOptionStatements(string code)
  51. {
  52. if (this.IsCSharp || (code == null))
  53. {
  54. return code;
  55. }
  56. StringBuilder builder = new StringBuilder(code);
  57. string str = "Option Strict Off";
  58. string newValue = "Option Strict On";
  59. string str3 = "Option Infer On";
  60. string str4 = "Option Compare Binary";
  61. int index = code.IndexOf(str, StringComparison.Ordinal);
  62. if (index != -1)
  63. {
  64. builder.Replace(str, newValue, index, str.Length);
  65. builder.Insert(index, str3 + Environment.NewLine);
  66. builder.Insert(index, str4 + Environment.NewLine);
  67. }
  68. return builder.ToString();
  69. }
  70. internal GeneratedCode GenerateCode()
  71. {
  72. string s = string.Empty;
  73. using (TextWriter writer = new StringWriter(CultureInfo.InvariantCulture))
  74. {
  75. this.FixUpCompileUnit(this.CompileUnit);
  76. this.Provider.GenerateCodeFromCompileUnit(this.CompileUnit, writer, this._options);
  77. s = this.FixupVBOptionStatements(writer.ToString());
  78. }
  79. return new GeneratedCode(CodeGenUtilities.StripAutoGenPrefix(s, this.IsCSharp), this.References);
  80. }
  81. internal CodeNamespace GetNamespace(CodeTypeDeclaration typeDecl)
  82. {
  83. string key = typeDecl.UserData["Namespace"] as string;
  84. CodeNamespace namespace2 = null;
  85. this._namespaces.TryGetValue(key, out namespace2);
  86. return namespace2;
  87. }
  88. private string GetNamespaceName(string namespaceName)
  89. {
  90. string str = namespaceName;
  91. if (!string.IsNullOrEmpty(this._rootNamespace) && namespaceName.Equals(this._rootNamespace, StringComparison.Ordinal))
  92. {
  93. return string.Empty;
  94. }
  95. if (!string.IsNullOrEmpty(this._rootNamespace) && namespaceName.StartsWith(this._rootNamespace + ".", StringComparison.Ordinal))
  96. {
  97. str = namespaceName.Substring(this._rootNamespace.Length + 1);
  98. }
  99. return str;
  100. }
  101. internal CodeNamespace GetOrGenNamespace(string namespaceName)
  102. {
  103. CodeNamespace namespace2 = null;
  104. if (string.IsNullOrEmpty(namespaceName))
  105. {
  106. return null;
  107. }
  108. string key = this.GetNamespaceName(namespaceName);
  109. if (!this._namespaces.TryGetValue(key, out namespace2))
  110. {
  111. namespace2 = new CodeNamespace(key);
  112. this._namespaces[key] = namespace2;
  113. foreach (string str2 in BusinessLogicClassConstants.FixedImports)
  114. {
  115. CodeNamespaceImport import = new CodeNamespaceImport(str2);
  116. namespace2.Imports.Add(import);
  117. }
  118. this.CompileUnit.Namespaces.Add(namespace2);
  119. }
  120. return namespace2;
  121. }
  122. private void Initialize()
  123. {
  124. System.CodeDom.Compiler.CodeGeneratorOptions options = new System.CodeDom.Compiler.CodeGeneratorOptions {
  125. IndentString = " ",
  126. VerbatimOrder = true,
  127. BlankLinesBetweenMembers = true,
  128. BracingStyle = "C"
  129. };
  130. this._options = options;
  131. }
  132. internal bool IsValidIdentifier(string identifier)
  133. {
  134. return (!string.IsNullOrEmpty(identifier) && this._provider.IsValidIdentifier(identifier));
  135. }
  136. internal System.CodeDom.Compiler.CodeGeneratorOptions CodeGeneratorOptions
  137. {
  138. get
  139. {
  140. return this._options;
  141. }
  142. }
  143. private CodeCompileUnit CompileUnit
  144. {
  145. get
  146. {
  147. return this._compileUnit;
  148. }
  149. }
  150. internal bool IsCSharp
  151. {
  152. get
  153. {
  154. return (this._provider is CSharpCodeProvider);
  155. }
  156. }
  157. internal CodeDomProvider Provider
  158. {
  159. get
  160. {
  161. return this._provider;
  162. }
  163. }
  164. internal IEnumerable<string> References
  165. {
  166. get
  167. {
  168. return this._references;
  169. }
  170. }
  171. internal string RootNamespace
  172. {
  173. get
  174. {
  175. return this._rootNamespace;
  176. }
  177. }
  178. }
  179. }