Add() public method

public Add ( CodeNamespaceImport value ) : void
value CodeNamespaceImport
return void
		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");
		}
		public void Add_Null () {
			CodeNamespaceImportCollection coll = new CodeNamespaceImportCollection ();
			coll.Add ((CodeNamespaceImport) null);
		}
		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;
		}
Example #5
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('.'))));
        }
Example #6
0
 protected virtual void AddDefaultImports(CodeNamespaceImportCollection imports)
 {
     imports.Add(new CodeNamespaceImport("System"));
 }
 protected void Rewrite(CodeNamespaceImportCollection target, CodeNamespaceImportCollection source, ref bool didRewrite)
 {
     foreach (CodeNamespaceImport item in source)
     {
         target.Add(this.Rewrite(item, ref didRewrite));
     }
 }
Example #8
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));
            }
        }
		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;
		}