反射出错!
Could not load file or assembly 'file:///C:\Users\Echo\AppData\Local\Temp\ocoe1m3m.dll' or one of its dependencies. 系统找不到指定的文件
代码如下:
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;
using System.CodeDom;
[Test]
         public void TestReflection()
         {
             StringBuilder strBd = new StringBuilder();
             strBd.Append("public    class    DynamicClass \n");
             strBd.Append("{\n");
             // 创建属性。        
             strBd.Append(propertyString("aaa"));
             strBd.Append(propertyString("bbb"));
             strBd.Append(propertyString("ccc"));
             strBd.Append("}");
             strBd.Append("}");
             // 创建编译器实例。        
             CSharpCodeProvider provider = new CSharpCodeProvider();
             // 设置编译参数。        
             CompilerParameters paras = new CompilerParameters();
             paras.GenerateExecutable = false;
             paras.GenerateInMemory = true;
             // 编译代码。        
             CompilerResults result = provider.CompileAssemblyFromSource(paras,strBd.ToString());
             // 获取编译后的程序集。        
             Assembly assembly = result.CompiledAssembly;
             object Class1 = assembly.CreateInstance("DynamicClass");
             ReflectionSetProperty(Class1, "aaa", 10);
             ReflectionGetProperty(Class1, "aaa");
             object Class2 = assembly.CreateInstance("DynamicClass");
             ReflectionSetProperty(Class1, "bbb", 20);
             ReflectionGetProperty(Class1, "bbb");
         }
         private static string propertyString(string propertyName)
         {
             StringBuilder sbProperty = new StringBuilder();
             sbProperty.Append(" private    int    _" + propertyName + "    =    0;\n");
             sbProperty.Append(" public    int    " + "" + propertyName + "\n");
             sbProperty.Append(" {\n");
             sbProperty.Append(" get{    return    _" + propertyName + ";}    \n");
             sbProperty.Append(" set{    _" + propertyName + "    =    value;    }\n");
             sbProperty.Append(" }");
             return sbProperty.ToString();
         }
         private static void ReflectionSetProperty(object objClass, string propertyName, int value)
         {
             PropertyInfo[] infos = objClass.GetType().GetProperties();
             foreach (PropertyInfo info in infos)
             {
                 if (info.Name == propertyName && info.CanWrite)
                 {
                     info.SetValue(objClass, value, null);
                 }
             }
         }
         private static void ReflectionGetProperty(object objClass, string propertyName)
         {
             PropertyInfo[] infos = objClass.GetType().GetProperties();
             foreach (PropertyInfo info in infos)
             {
                 if (info.Name == propertyName && info.CanRead)
                 {
                     System.Console.WriteLine(info.GetValue(objClass, null)