日期:2014-05-18  浏览次数:20888 次

求解,关于C#引用类的
代码:
文件1:
C# code

    class Class2
    {
        public Class2()
        {
            Console.WriteLine("class2");
        }
    }



文件2://用的是动态编译,代码可以不看,只看 红色字体的部分

C# code

       static void Main(string[] args)
        {



            CSharpCodeProvider codeProvide = new CSharpCodeProvider();
            ICodeCompiler codeCompiler = codeProvide.CreateCompiler();
            CompilerParameters compilerParameters = new CompilerParameters();
            compilerParameters.ReferencedAssemblies.Add("System.dll");
            compilerParameters.ReferencedAssemblies.Add("System.Core.dll");
            compilerParameters.GenerateExecutable = false;
            compilerParameters.GenerateInMemory = true;
            CompilerResults cr = codeCompiler.CompileAssemblyFromSource(compilerParameters, GenerateCode());
            if (cr.Errors.HasErrors)
            {
                Console.WriteLine("编译错误:");
                foreach (CompilerError err in cr.Errors)
                {
                    Console.WriteLine(err.ErrorText);
                }
            }
            else
            {
                Assembly assembly = cr.CompiledAssembly;
                object helloword = assembly.CreateInstance("PINQ_test_ConsoleApplication.HelloWorld");
                MethodInfo methodInfo = helloword.GetType().GetMethod("OutPut");
                object[] objs = new object[1];
                objs[0] = "fuckYou";
                char[] cc = (char[])methodInfo.Invoke(helloword, objs);
                Console.WriteLine(cc[0].ToString());
                Console.WriteLine(new String(cc));
            }

            Console.ReadLine();
        }

  
     

        static string GenerateCode()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("using System;using System.Linq;");
            sb.Append(Environment.NewLine);
            sb.Append("namespace PINQ_test_ConsoleApplication");
            sb.Append(Environment.NewLine);
            sb.Append("{");
            sb.Append(Environment.NewLine);
            sb.Append("      public class HelloWorld");
            sb.Append(Environment.NewLine);
            sb.Append("      {");
            sb.Append(Environment.NewLine);
            sb.Append("          public char[] OutPut(string str)");
            sb.Append(Environment.NewLine);
            sb.Append("          {");
            sb.Append(Environment.NewLine);
 [color=#FF0000]           sb.Append("Class2 cc=new Class2();               return str.ToArray();");[/color]
            sb.Append(Environment.NewLine);
            sb.Append("          }");
            sb.Append(Environment.NewLine);
            sb.Append("      }");
            sb.Append(Environment.NewLine);
            sb.Append("}");

            string code = sb.ToString();
            //Console.WriteLine(code);
            //Console.WriteLine();

            return code;
        }




这个时候显示的是错误的,因为这是两个文件,虽然我定义了同一个命名空间,但是,系统好像并不把他们当做一个命名空间处理,

请问,应该如何解决?

------解决方案--------------------
错误提示是什么?你加上完全的命名空间限定名看看,比如

XXXX.Class2 cc=new XXXX.Class2();

看看还有没有错,
------解决方案--------------------
compilerParameters.ReferencedAssemblies.Add("System.dll");

你没加DLL的引用,把引用加上
------解决方案--------------------
如果是内部的类直接调用就行,外部的先添加引用DLL文件