当前位置: 代码迷 >> ASP.NET >> 50%身价求解决方案解决办法
  详细解决方案

50%身价求解决方案解决办法

热度:1040   发布时间:2013-02-25 00:00:00.0
50%身价求解决方案
我有个大概的想法:
我又若干Class文件,作为文本文件的方式存储于网站的某文件夹或数据库(网站程序自身不调用);
有什么方法能够实现这些Class在服务器端于必要的时候通过程序编译为dll文件?

求大神们提供思路

------解决方案--------------------------------------------------------
可以参考

如何以编程方式编译使用 C# 编译器代码


http://blogs.msdn.com/b/saveenr/archive/2009/08/11/a-walkthrough-of-dynamically-compiling-c-code.aspx
http://support.microsoft.com/kb/304655

http://msdn.microsoft.com/zh-cn/library/microsoft.csharp.csharpcodeprovider%28v=vs.80%29.aspx


搜索
------解决方案--------------------------------------------------------
C# code
public class Program    {        static void Main(string[] args)        {            // 1.CSharpCodePrivoder            CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();            // 2.ICodeComplier            ICodeCompiler objICodeCompiler = objCSharpCodePrivoder.CreateCompiler();            //CodeDomProvider objICodeCompiler = (ICodeCompiler)objCSharpCodePrivoder.CreateCompiler();            // 3.CompilerParameters            CompilerParameters objCompilerParameters = new CompilerParameters();            objCompilerParameters.ReferencedAssemblies.Add("System.dll");            //objCompilerParameters.ReferencedAssemblies.Add("System.dll");            objCompilerParameters.GenerateExecutable = true;            objCompilerParameters.GenerateInMemory = false;            objCompilerParameters.CompilerOptions = "/out:d:/b.exe /target:exe";                        // 4.CompilerResults            CompilerResults cr = objICodeCompiler.CompileAssemblyFromSource(objCompilerParameters, GenerateCode());            //cr.PathToAssembly = "d:/a.exe";                        if (cr.Errors.HasErrors)            {                Console.WriteLine("编译错误:");                foreach (CompilerError err in cr.Errors)                {                    Console.WriteLine(err.ErrorText);                }            }            //Console.WriteLine(cr.PathToAssembly);            //else            //{            //    // 通过反射,调用HelloWorld的实例            //    Assembly objAssembly = cr.CompiledAssembly;            //    object objHelloWorld = objAssembly.CreateInstance("DynamicCodeGenerate.HelloWorld");            //    MethodInfo objMI = objHelloWorld.GetType().GetMethod("Main");            //    Console.WriteLine(objMI.Invoke(objHelloWorld, null));            //}            Console.ReadLine();                    }        static string GenerateCode()        {            StringBuilder sb = new StringBuilder();            sb.Append("using System;using System.Text.RegularExpressions;");            sb.Append(Environment.NewLine);            sb.Append("namespace DynamicCodeGenerate");            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("          static void Main(string[] args)");            sb.Append(Environment.NewLine);            sb.Append("          {");            sb.Append(Environment.NewLine);            sb.Append("Regex rgx = new Regex();               Console.Write(\"Hello world!\");");            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;        }    }
  相关解决方案