生成控制台应用程序、窗口程序都是把cs文件编译好了,更改cs文件的值后需要重新编译才能更改。
请问前辈,有没有办法使它能像aspx那样调用没编译的cs文件,谢谢!!(前段时间太豪爽,现在没分了)

解决方案 »

  1.   

    你可以生成 但是还想在程序运行中使用 哥哥 现在的cpu不支持在运行中修改程序地 
      

  2.   

    用CODEDOM好了,
    一会给你写个小例子...using System;
    using System.Reflection;
    using Microsoft.CSharp;
    using System.CodeDom.Compiler;...C#4.0中就更简单了...
      

  3.   

    CODEDOM就是动态编译代码吧?速度很慢的是不是,我在自己的机里试很慢
      

  4.   

    动态编译非常不慢,
    还是解决"很慢场景"的通用办法一般来说,真接使用反射会慢上数百倍,
    所以改进的办法就是在反射上嫁接动态编译.C#4.0中,接近正常速度,
    其实ASPX就是这么干的.        string TestString = @"
            public class Test
            {
                public static string Test()
                {
                    string Str = ""test it !!!!"";
                    return Str;
                }
            }";        CompilerParameters compilerParams = new CompilerParameters();
            compilerParams.GenerateInMemory = true;
            compilerParams.IncludeDebugInformation = false;
            compilerParams.ReferencedAssemblies.Add("mscorlib.dll");
            compilerParams.ReferencedAssemblies.Add("System.dll");
            ICodeCompiler compiler = new CSharpCodeProvider().CreateCompiler();
            CompilerResults results = compiler.CompileAssemblyFromSource(compilerParams, TestString);
            Assembly asm = results.CompiledAssembly;
            object objMyTestClass = asm.CreateInstance("Test");
            Type MyTestClassType = objMyTestClass.GetType();
            Console.WriteLine(MyTestClassType.GetMethod("Test").Invoke(objMyTestClass, null));