Inheritance: System.Collections.IList, System.Collections.ICollection, System.Collections.IEnumerable
 private CodeNamespace(SerializationInfo info, StreamingContext context)
 {
     this.imports = new CodeNamespaceImportCollection();
     this.comments = new CodeCommentStatementCollection();
     this.classes = new CodeTypeDeclarationCollection();
     this.namespaces = new CodeNamespaceCollection();
 }
 private CodeNamespace(SerializationInfo info, StreamingContext context)
 {
     this.imports    = new CodeNamespaceImportCollection();
     this.comments   = new CodeCommentStatementCollection();
     this.classes    = new CodeTypeDeclarationCollection();
     this.namespaces = new CodeNamespaceCollection();
 }
 public CodeNamespace()
 {
     this.imports    = new CodeNamespaceImportCollection();
     this.comments   = new CodeCommentStatementCollection();
     this.classes    = new CodeTypeDeclarationCollection();
     this.namespaces = new CodeNamespaceCollection();
 }
 public CodeNamespace()
 {
     this.imports = new CodeNamespaceImportCollection();
     this.comments = new CodeCommentStatementCollection();
     this.classes = new CodeTypeDeclarationCollection();
     this.namespaces = new CodeNamespaceCollection();
 }
		public void Constructor0 ()
		{
			CodeNamespaceImportCollection coll = new CodeNamespaceImportCollection ();
			Assert.IsFalse (((IList) coll).IsFixedSize, "#1");
			Assert.IsFalse (((IList) coll).IsReadOnly, "#2");
			Assert.AreEqual (0, coll.Count, "#3");
			Assert.IsFalse (((ICollection) coll).IsSynchronized, "#4");
			Assert.IsNull (((ICollection) coll).SyncRoot, "#5");
		}
		public void Constructor0_Deny_Unrestricted ()
		{
			CodeNamespaceImportCollection coll = new CodeNamespaceImportCollection ();
			coll.Add (cni);
			Assert.AreSame (cni, coll[0], "this[int]");
			coll[0] = cni;
			coll.Clear ();
			coll.AddRange (array);
			Assert.IsNotNull (coll.GetEnumerator (), "GetEnumerator");
		}
		public void Add ()
		{
			CodeNamespaceImport ni1 = new CodeNamespaceImport ("A");
			CodeNamespaceImport ni2 = new CodeNamespaceImport ("B");
			CodeNamespaceImport ni3 = new CodeNamespaceImport ("b");
			CodeNamespaceImport ni4 = new CodeNamespaceImport ("B");

			CodeNamespaceImportCollection coll = new CodeNamespaceImportCollection ();
			coll.Add (ni1);
			Assert.AreEqual (1, coll.Count, "#1");
			Assert.AreEqual (0, ((IList) coll).IndexOf (ni1), "#2");

			coll.Add (ni2);
			Assert.AreEqual (2, coll.Count, "#3");
			Assert.AreEqual (1, ((IList) coll).IndexOf (ni2), "#4");

			coll.Add (ni3);
			Assert.AreEqual (2, coll.Count, "#5");
			Assert.AreEqual (-1, ((IList) coll).IndexOf (ni3), "#6");

			coll.Add (ni4);
			Assert.AreEqual (2, coll.Count, "#7");
			Assert.AreEqual (-1, ((IList) coll).IndexOf (ni4), "#8");
		}
Exemplo n.º 8
0
 private void AddImportsForCodeTypeReferenceCollection(CodeNamespaceImportCollection curImports, CodeTypeReferenceCollection ctrc)
 {
     foreach (CodeTypeReference ctr in ctrc) {
         MaybeAddImport(curImports, ctr);
     }
 }
		public void AddRange_Null_Item()
		{
			CodeNamespaceImportCollection coll = new CodeNamespaceImportCollection();
			coll.AddRange(new CodeNamespaceImport[] { null });
		}
		static CodeCompileUnit ParseInternal (TextReader codeStream)
		{
			var tree = SyntaxTree.Parse (codeStream);

			if (tree.Errors.Count > 0)
				throw new ArgumentException ("Stream contained errors.");

			var convertVisitor = new CodeDomConvertVisitor ();

			CodeCompileUnit ccu = convertVisitor.Convert (Compilation.Value, tree, tree.ToTypeSystem ());
			
			//C# parser seems to insist on putting imports in the "Global" namespace; fix it up
			for (int i = 0; i < ccu.Namespaces.Count; i++) {
				CodeNamespace global = ccu.Namespaces [i];
				if ((global.Name == "Global") && (global.Types.Count == 0)) {
					global.Name = "";
					ccu.Namespaces.RemoveAt (i);
					ccu.Namespaces.Insert (0, global);
					
					//clear out repeat imports...
					for (int j = 1; j < ccu.Namespaces.Count; j++) {
						CodeNamespace cn = ccu.Namespaces [j];
						
						//why can't we remove imports? will have to collect ones to keep
						//then clear and refill
						CodeNamespaceImportCollection imps = new CodeNamespaceImportCollection ();
						
						for (int m = 0; m < cn.Imports.Count; m++) {
							bool found = false;
							
							for (int n = 0; n < global.Imports.Count; n++)
								if (global.Imports [n] == cn.Imports [m])
									found = true;
						
							if (!found)
								imps.Add (cn.Imports [m]);
						}
						
						cn.Imports.Clear ();
						
						foreach (CodeNamespaceImport imp in imps)
							cn.Imports.Add (imp);
					}
					
					break;
				}
			}
			return ccu;
		}
		static CodeCompileUnit ParseInternal (TextReader codeStream)
		{
			IParser parser = ParserFactory.CreateParser (
				SupportedLanguage.CSharp,
				codeStream);
			parser.ParseMethodBodies = true;
			parser.Parse ();
			
			if (parser.Errors.Count > 0)
				throw new ArgumentException (parser.Errors.ErrorOutput);
			
			CodeDomVisitor cdv = new CodeDomVisitor (); // new CodeDomVisitor (parser.Lexer.SpecialTracker.CurrentSpecials);
			parser.CompilationUnit.AcceptVisitor (cdv, null);
			
			parser.Dispose ();
			
			CodeCompileUnit ccu = cdv.codeCompileUnit;
			
			//C# parser seems to insist on putting imports in the "Global" namespace; fix it up
			for (int i = 0; i < ccu.Namespaces.Count; i++) {
				CodeNamespace global = ccu.Namespaces [i];
				if ((global.Name == "Global") && (global.Types.Count == 0)) {
					global.Name = "";
					ccu.Namespaces.RemoveAt (i);
					ccu.Namespaces.Insert (0, global);
					
					//clear out repeat imports...
					for (int j = 1; j < ccu.Namespaces.Count; j++) {
						CodeNamespace cn = ccu.Namespaces [j];
						
						//why can't we remove imports? will have to collect ones to keep
						//then clear and refill
						CodeNamespaceImportCollection imps = new CodeNamespaceImportCollection ();
						
						for (int m = 0; m < cn.Imports.Count; m++) {
							bool found = false;
							
							for (int n = 0; n < global.Imports.Count; n++)
								if (global.Imports [n] == cn.Imports [m])
									found = true;
						
							if (!found)
								imps.Add (cn.Imports [m]);
						}
						
						cn.Imports.Clear ();
						
						foreach (CodeNamespaceImport imp in imps)
							cn.Imports.Add (imp);
					}
					
					break;
				}
			}
			return ccu;
		}
		public void AddRange_Null_Array ()
		{
			CodeNamespaceImportCollection coll = new CodeNamespaceImportCollection ();
			coll.AddRange ((CodeNamespaceImport[]) null);
		}
Exemplo n.º 13
0
        private void AddImportsForTypeDeclaration(CodeNamespaceImportCollection curImports, CodeTypeDeclaration ctd)
        {
            AddImportsForCodeTypeReferenceCollection(curImports, ctd.BaseTypes);

            foreach (CodeTypeMember member in ctd.Members) {
                CodeMemberProperty prop;
                CodeMemberMethod meth;
                CodeMemberField field;
                CodeMemberEvent evnt;
                CodeTypeDeclaration innerType;

                if ((prop = member as CodeMemberProperty) != null) {
                    AddImportsForProperty(curImports, prop);
                } else if ((evnt = member as CodeMemberEvent) != null) {
                    AddImportsForEvent(curImports, evnt);
                } else if ((field = member as CodeMemberField) != null) {
                    AddImportsForField(curImports, field);
                } else if ((meth = member as CodeMemberMethod) != null) {
                    AddImportsForMethod(curImports, meth);
                } else if ((innerType = member as CodeTypeDeclaration) != null) {
                    AddImportsForTypeDeclaration(curImports, innerType);
                }
            }
        }
Exemplo n.º 14
0
        private void MaybeAddImport(CodeNamespaceImportCollection curImports, CodeTypeReference reference)
        {
            string typeName = reference.BaseType;

            if (reference.BaseType == "System.Object" ||
                reference.BaseType == "System.String" ||
                reference.BaseType == "System.Int32" ||
                reference.BaseType == "System.Double" ||
                reference.BaseType == "System.Void")
                return;

            int firstDot;
            if((firstDot = typeName.IndexOf('.')) == -1){
                return;
            }

            string typeNs = typeName.Substring(0, firstDot);

            foreach (CodeNamespaceImport cni in curImports) {
                if (cni.Namespace.StartsWith(typeNs))
                    return;
            }

            Console.WriteLine(reference.BaseType);
            curImports.Add(new CodeNamespaceImport(typeName.Substring(0, typeName.LastIndexOf('.'))));
        }
Exemplo n.º 15
0
 private void AddImportsForMethod(CodeNamespaceImportCollection imports, CodeMemberMethod method)
 {
     MaybeAddImport(imports, method.ReturnType);
 }
Exemplo n.º 16
0
 private void AddImportsForProperty(CodeNamespaceImportCollection imports, CodeMemberProperty prop)
 {
     MaybeAddImport(imports, prop.Type);
 }
Exemplo n.º 17
0
		public static CodeTypeReference CreateShortCodeTypeReference(Type type, CodeNamespaceImportCollection imports)
		{
			var result = new CodeTypeReference(type);

			Shortify(result, type, imports);

			return result;
		}
 /// <summary>
 /// Visits a <see cref="CodeNamespaceImportCollection"/>.
 /// </summary>
 /// <param name="codeNamespaceImportCollection">The <see cref="CodeNamespaceImportCollection"/> to visit.</param>
 protected virtual void VisitCodeNamespaceImportCollection(CodeNamespaceImportCollection codeNamespaceImportCollection)
 {
     // Visit all of the CodeNamespaceImport items in the collection.
     foreach (CodeNamespaceImport item in codeNamespaceImportCollection.Cast<CodeNamespaceImport>())
     {
         this.VisitCodeNamespaceImport(item);
     }
 }
		public void Add_Null () {
			CodeNamespaceImportCollection coll = new CodeNamespaceImportCollection ();
			coll.Add ((CodeNamespaceImport) null);
		}
Exemplo n.º 20
0
        void AddConditionalImports(CodeNamespaceImportCollection imports,
                string firstImport,
                string conditional,
                string[] importsIfTrue,
                string[] importsIfFalse,
                string lastImport)
        {
            if (Provider is CSharpCodeProvider)
            {
                // HACK HACK HACK
                // Would be better if CodeDom actually supported conditional compilation constructs...
                // This is predecated upon CSharpCodeGenerator.GenerateNamespaceImport() being implemented as:
                //      output.Write ("using ");
                //      output.Write (GetSafeName (import.Namespace));
                //      output.WriteLine (';');
                // Thus, with "crafty" execution of the namespace, we can stuff arbitrary text in there...

                var block = new StringBuilder();
                // No 'using', as GenerateNamespaceImport() writes it.
                block.Append(firstImport).Append(";").Append(Environment.NewLine);
                block.Append("#if ").Append(conditional).Append(Environment.NewLine);
                foreach (var ns in importsIfTrue)
                    block.Append("\tusing ").Append(ns).Append(";").Append(Environment.NewLine);
                block.Append("#else   // ").Append(conditional).Append(Environment.NewLine);
                foreach (var ns in importsIfFalse)
                    block.Append("\tusing ").Append(ns).Append(";").Append(Environment.NewLine);
                block.Append("#endif  // ").Append(conditional).Append(Environment.NewLine);
                block.Append("\tusing ").Append(lastImport);
                // No ';', as GenerateNamespaceImport() writes it.

                imports.Add(new CodeNamespaceImport(block.ToString()));
            }
            else if (Provider is VBCodeProvider)
            {
                // HACK HACK HACK
                // Would be better if CodeDom actually supported conditional compilation constructs...
                // This is predecated upon VBCodeGenerator.GenerateNamespaceImport() being implemented as:
                //      output.Write ("Imports ");
                //      output.Write (import.Namespace);
                //      output.WriteLine ();
                // Thus, with "crafty" execution of the namespace, we can stuff arbitrary text in there...

                var block = new StringBuilder();
                // No 'Imports', as GenerateNamespaceImport() writes it.
                block.Append(firstImport).Append(Environment.NewLine);
                block.Append("#If ").Append(conditional).Append(" Then").Append(Environment.NewLine);
                foreach (var ns in importsIfTrue)
                    block.Append("Imports ").Append(ns).Append(Environment.NewLine);
                block.Append("#Else     ' ").Append(conditional).Append(Environment.NewLine);
                foreach (var ns in importsIfFalse)
                    block.Append("Imports ").Append(ns).Append(Environment.NewLine);
                block.Append("#End If   ' ").Append(conditional).Append(Environment.NewLine);
                block.Append("Imports ").Append(lastImport);
                // No newline, as GenerateNamespaceImport() writes it.

                imports.Add(new CodeNamespaceImport(block.ToString()));
            }
            else
            {
                // Default to using the DbLinq imports
                imports.Add(new CodeNamespaceImport(firstImport));
                foreach (var ns in importsIfTrue)
                    imports.Add(new CodeNamespaceImport(ns));
                imports.Add(new CodeNamespaceImport(lastImport));
            }
        }
Exemplo n.º 21
0
 private UsedNamespaceList Translate(CodeNamespaceImportCollection imports){
   int n = imports == null ? 0 : imports.Count;
   UsedNamespaceList usedNspaces = new UsedNamespaceList(n);
   for (int i = 0; i < n; i++)
     usedNspaces.Add(this.Translate(imports[i]));
   return usedNspaces;
 }
Exemplo n.º 22
0
 protected virtual void AddDefaultImports(CodeNamespaceImportCollection imports)
 {
     imports.Add(new CodeNamespaceImport("System"));
 }
Exemplo n.º 23
0
 protected void Rewrite(CodeNamespaceImportCollection target, CodeNamespaceImportCollection source, ref bool didRewrite)
 {
     foreach (CodeNamespaceImport item in source)
     {
         target.Add(this.Rewrite(item, ref didRewrite));
     }
 }
		public void AddRange ()
		{
			CodeNamespaceImport ni1 = new CodeNamespaceImport ("A");
			CodeNamespaceImport ni2 = new CodeNamespaceImport ("B");
			CodeNamespaceImport ni3 = new CodeNamespaceImport ("b");
			CodeNamespaceImport ni4 = new CodeNamespaceImport ("B");
			CodeNamespaceImport ni5 = new CodeNamespaceImport ("C");

			CodeNamespaceImportCollection coll = new CodeNamespaceImportCollection ();
			coll.AddRange (new CodeNamespaceImport[] {ni1, ni2});
			Assert.AreEqual (2, coll.Count, "#1");
			Assert.AreEqual (0, ((IList) coll).IndexOf (ni1), "#2");
			Assert.AreEqual (1, ((IList) coll).IndexOf (ni2), "#3");

			coll.AddRange (new CodeNamespaceImport[] { ni3, ni4, ni5 });
			Assert.AreEqual (3, coll.Count, "#4");
			Assert.AreEqual (0, ((IList) coll).IndexOf (ni1), "#5");
			Assert.AreEqual (1, ((IList) coll).IndexOf (ni2), "#6");
			Assert.AreEqual (-1, ((IList) coll).IndexOf (ni3), "#7");
			Assert.AreEqual (-1, ((IList) coll).IndexOf (ni4), "#8");
			Assert.AreEqual (2, ((IList) coll).IndexOf (ni5), "#9");
		}
Exemplo n.º 25
0
 private void AddImportsForEvent(CodeNamespaceImportCollection imports, CodeMemberEvent evnt)
 {
     MaybeAddImport(imports, evnt.Type);
 }
Exemplo n.º 26
0
		public static void Shortify(CodeTypeReference typeReference, Type type, CodeNamespaceImportCollection imports)
		{
			if (typeReference.ArrayRank > 0)
			{
				Shortify(typeReference.ArrayElementType, type, imports);
				return;
			}

			if (type.Namespace != null && imports.Cast<CodeNamespaceImport>()
				.Any(cni => cni.Namespace == type.Namespace))
			{
				var prefix = type.Namespace + '.';

				if (prefix != null)
				{
					var pos = typeReference.BaseType.IndexOf(prefix);
					if (pos == 0)
					{
						typeReference.BaseType = typeReference.BaseType.Substring(prefix.Length);
					}
				}
			}
		}
Exemplo n.º 27
0
 private void AddImportsForField(CodeNamespaceImportCollection imports, CodeMemberField field)
 {
     MaybeAddImport(imports, field.Type);
 }