public static Assembly NewAssembly(ArrayList arraylist)
        {
            CSharpCodeProvider provider = new CSharpCodeProvider();
            ICodeCompiler iCodeCompiler =provider.CreateCompiler();
            CompilerParameters paras = new CompilerParameters();
            paras.GenerateExecutable = false;
            paras.GenerateInMemory = true;
            //paras.ReferencedAssemblies.Add("System.dll");
            //paras.ReferencedAssemblies.Add("System..Collections.Generic");
            //paras.ReferencedAssemblies.Add("System.Test");
            //paras.ReferencedAssemblies.Add("System.ComponentModel");
            //CodeSnippetCompileUnit cu = new CodeSnippetCompileUnit();
            //CompilerResults cr = iCodeCompiler.CompileAssemblyFromDom(paras, cu);
            StringBuilder classSource = new StringBuilder();
            classSource.Append("public class DynamicClass\n");
            classSource.Append("{");   
            for (int i = 0; i < arraylist.Count; i++)
            {
                classSource.Append(propertyString(arraylist[i].ToString()));
            }         
            classSource.Append("\n}");
            System.Diagnostics.Debug.WriteLine(classSource.ToString());
            CompilerResults result = provider.CompileAssemblyFromSource(paras, classSource.ToString());
            Assembly assembly = result.CompiledAssembly;            return assembly;
        }
这样的代码,是没有问题的.但是如果把注销的代码加入,使用注销的代码,那么总是提示"Could not load file or assembly 'file:///C:\Documents and Settings\Admin\Local Settings\Temp\08v8tsod.dll' or one of its dependencies. 系统找不到指定的文件。"
请各位高手指点.十分感谢.

解决方案 »

  1.   

    因为我想把这个类假如名称空间,引用SYSTEM,SYSTEM.TEXT,SYSTEM.COMPONENTMODEL等信息.所以想加入这些.调用的Class1 = assembly.CreateInstance("DynamicClass")也加上名称空间的:Class1 = assembly.CreateInstance("WindowsAppTest.DynamicClass");总是出错,不知道为什么,请各位高手给点提示。十分感谢
      

  2.   

    你好像多写了一个".":paras.ReferencedAssemblies.Add("System..Collections.Generic");
    -->
    paras.ReferencedAssemblies.Add("System.Collections.Generic");
      

  3.   

    你在使用CompilerResults编译的时候,实际上出现了编译错误,因此没有生成临时性的类库。你可以查看一下result.Errors里面说明了错误的原因。不过,paras.ReferencedAssemblies.Add()参数是类库的文件路径,不是像我们使用using命名空间。
      

  4.   

    我又看了下://有了这个后面的System..Collections.Generic及System.ComponentModel都不要了
    paras.ReferencedAssemblies.Add("System.dll"); 
    //去掉,这个不是DLL文件,而是System里的命名空间
    paras.ReferencedAssemblies.Add("System..Collections.Generic"); 
    //这个可能是你的自己的类库,这里需要的是DLL所在的路径加文件名,比如:c:\aaa\aaa\test.dll
    paras.ReferencedAssemblies.Add("System.Test"); 
    //去掉,这个不是DLL文件,而是System里的命名空间
    paras.ReferencedAssemblies.Add("System.ComponentModel"); 
      

  5.   

    你如果要使用使用
    System..Collections.Generic

    System.ComponentModel
    两个命名空间,可以在你的代码里写上:classSource.Append("using System.Collections.Generic;\n");
    classSource.Append("System.ComponentModel;\n");
    classSource.Append("namespace youNS\n");
    classSource.Append("{");
    classSource.Append("public class DynamicClass\n");
    classSource.Append("{");
    for (int i = 0; i < arraylist.Count; i++)
    {
    classSource.Append(propertyString(arraylist[i].ToString()));
    }
    classSource.Append("\n}");
    classSource.Append("\n}");
      

  6.   

    首先十分感谢您的回复.
    关于多写了一个"."这是在帖子上的失误,不好意思.在实际VS2005中是没有的.
    classSource.Append("using System.Collections.Generic;\n");
    classSource.Append("System.ComponentModel;\n");
    ...
    ...
    ...
    前两天,当我使用这种方法的时候好象也会是类似错误.说找不到文件.
    十分感谢您的回复.我正在研究您的代码.
    也感谢上次您对我PROPERTYGIRD控件疑问的解答.谢谢
      

  7.   

    在使用classSource.Append方法:
    classSource.Append("using System.Collections.Generic;\n");
    classSource.Append("System.ComponentModel;\n");
    classSource.Append("namespace youNS\n");
    生成如下:
    using System.Collections.Generic;
    System.ComponentModel;
    namespace WindowsAppTes
    {
    public class DynamicClass
    {
     
    private string _unitId;
    public string unitId
    {
    get{return _unitId;} 
    set{ _unitId = value; }

    private string _unitIdd;
    public string unitIdd
    {
    get{return _unitIdd;} 
    set{ _unitIdd = value; }
    }
    }
    }
    但是assembly这里还是提示:Could not load file or assembly 'file:///C:\Documents and Settings\Admin\Local Settings\Temp\pfp9jelx.dll' or one of its dependencies. 系统找不到指定的文件。
      

  8.   

    更正:classSource.Append("using System.ComponentModel;\n");笔误.不好意思
      

  9.   

    你要看一下Errors里的内容。这是报出错误的地方。把Errors里的问题都解决了类库就可以生成了并使用了。
      

  10.   

    string iiiiii = result.Errors[0].ErrorText.ToString();
    显示内容是:The type or namespace name 'ComponentModel' does not exist in the namespace 'System' (are you missing an assembly reference?)
    可是我前面有写:
    classSource.Append("using System.Collections.Generic;\n");
                classSource.Append("using System;\n");
                classSource.Append("using System.Text;\n");
                classSource.Append("using System.ComponentModel;\n");
    这个错误有点迷惑.我生成的classSource字符:
    using System.Collections.Generic;
    using System;
    using System.Text;
    using System.ComponentModel;
    namespace WindowsAppTest
    {
    public class DynamicClass
    {
     
    private string _unitId;
    public string unitId
    {
    get{return _unitId;} 
    set{ _unitId = value; }

    private string _unitIdd;
    public string unitIdd
    {
    get{return _unitIdd;} 
    set{ _unitIdd = value; }
    }
    }
    }
      

  11.   

    classSource.Append("using System.Collections.Generic;\n"); 
                classSource.Append("using System;\n"); 
                classSource.Append("using System.Text;\n"); 
      [color=#FF6600]          classSource.Append("using System.ComponentModel;\n"); [/color]
    如果把这个红色的这一行删除,完全可以正常运行.
    如果加入这一行,总是说我提示:The type or namespace name 'ComponentModel' does not exist in the namespace 'System' (are you missing an assembly reference?)
      

  12.   

    paras.ReferencedAssemblies.Add("System.dll"); 
    这句还有没?
      

  13.   

     //paras.ReferencedAssemblies.Add("System");
                //paras.ReferencedAssemblies.Add("System.Collections.Generic");
                //paras.ReferencedAssemblies.Add("System.Test");
                //paras.ReferencedAssemblies.Add("System.ComponentModel");
                //CodeNamespace codeNamespace = new CodeNamespace("WindowsAppTest");
                //CodeCompileUnit codeCompileUnit = new CodeCompileUnit();
                //codeCompileUnit.Namespaces.Add(codeNamespace);
    这些全都注掉了
    发现仅仅使用classResource.Append("....")就可以使用了.
    调用都是没问题的
    现在就是14,15楼的问题没办法解决
    谢谢关注:)
      

  14.   

    加上这句
    paras.ReferencedAssemblies.Add("System.dll");  
      

  15.   

    还没有解决?我在我机子上测试通过,楼主看一下有什么不一样的地方:
    public static Assembly NewAssembly(ArrayList arraylist)
    {
    CSharpCodeProvider provider = new CSharpCodeProvider();
    CompilerParameters paras = new CompilerParameters();
    paras.GenerateExecutable = false;
    paras.GenerateInMemory = true;
    paras.ReferencedAssemblies.Add("System.dll"); CodeSnippetCompileUnit cu = new CodeSnippetCompileUnit();
    StringBuilder classSource = new StringBuilder();
    classSource.Append("using System;\n");
    classSource.Append("using System.Collections.Generic;\n");
    classSource.Append("using System.ComponentModel;\n");
    classSource.Append("namespace youNS\n");
    classSource.Append("{\n");
    classSource.Append("\tpublic class DynamicClass\n");
    classSource.Append("\t{\n");
    for (int i = 0; i < arraylist.Count; i++)
    {
    classSource.Append(propertyString(arraylist[i].ToString()));
    }

    classSource.Append("\t}\n");
    classSource.Append("}\n");
    CompilerResults result = provider.CompileAssemblyFromSource(paras, classSource.ToString());
    Assembly assembly = result.CompiledAssembly; return assembly;
    }
      

  16.   

    今天刚刚测试通过。十分感谢您的帮助.
    在CSDN交流的同时还可以交到一些朋友.真是高兴.
    方法也和您的一样.
    少了    paras.ReferencedAssemblies.Add("System.dll");这么一句.没有把System.dll加入.
    十分感谢您的回复.
      

  17.   

    也十分感谢lake_cx 的回复.您的回复正是我缺少的重要的部分,对我的项目非常重要.谢谢.