locked
Error CS0006: Metadata file 'System.Collections.dll' could not be found RRS feed

  • Question

  • User-541184106 posted

    I have a requirement were i need to create dynamic assembly at runtime. If i use simple addition function which retun interger sum then its working fine but when i use LINQ functionality inside my function then CompilerResults as error.

    I already add respective assembly references and do many work around but none of them works.

    Does anyone knows how to solve this issue.  

    protected void Button4_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" public class MyClass {   ");
    	sb.AppendLine();
            sb.Append("  public  MyClass(){ } ");
    	sb.AppendLine();
            sb.Append("  public List<int> GetLinq()  ");
    	sb.AppendLine();
            sb.Append("  {  ");
    	sb.AppendLine();
            sb.Append("   List<int> lst = new List<int> { 1,2,3};");
    	sb.AppendLine();
            sb.Append("   var source = lst.AsQuerable(); ");
    	sb.AppendLine();
            sb.Append("   var obj = from i in source select i; ");
    	sb.AppendLine();
            sb.Append("   return obj.ToList<int>();
    	sb.AppendLine();
            sb.Append("  } ");
    	sb.AppendLine();
            sb.Append(" } ");
    	sb.AppendLine();
            //
            string functionName = "GetLinq";
           
    
            CodeDomProvider provider;
            string language = "cs";
    
            Dictionary<string, string> compilerInfo = new Dictionary<string, string>();
            compilerInfo.Add("CompilerVersion", "v3.5");
    
            // Check for a provider corresponding to the input language.
            if (CodeDomProvider.IsDefinedLanguage(language))
            {
                
                provider = new Microsoft.CSharp.CSharpCodeProvider(compilerInfo);
    
                
                CompilerInfo langCompilerInfo = CodeDomProvider.GetCompilerInfo(language);
                CompilerParameters langCompilerConfig = langCompilerInfo.CreateDefaultCompilerParameters();
                langCompilerConfig.ReferencedAssemblies.Add("System.dll");
                langCompilerConfig.ReferencedAssemblies.Add("System.Core.dll");
                langCompilerConfig.ReferencedAssemblies.Add("System.Data.dll");
                langCompilerConfig.ReferencedAssemblies.Add("System.Xml.dll");
                langCompilerConfig.ReferencedAssemblies.Add("System.Collections.dll");
                langCompilerConfig.GenerateInMemory = true;
                langCompilerConfig.TreatWarningsAsErrors = false;
                langCompilerConfig.CompilerOptions = "/optimize";
                //
                CompilerResults results = provider.CompileAssemblyFromSource(langCompilerConfig, sb.ToString());
                if (results.Errors.Count > 0)
                {
                    Response.Write("Errors Occured" + results.Errors.Count);
                    return;
                }
    
                else
                {
                    Type dynamicType;
                    dynamicType = results.CompiledAssembly.GetType("MyClass");
                    MethodInfo method;
                    method = dynamicType.GetMethod(functionName);
                    var obj =  method.Invoke(null, null);
                }
    
            }
            else
            {
                // Tell the user that the language provider was not found.
                Response.Write("There is no support for input language \""+language+"\".");
                //return null;
            }
    
        }


    Tuesday, May 21, 2013 6:10 AM

Answers

  • User-541184106 posted

    Hi Paul,

            Thanks for reply, but i resolve my issue by adding reference to System.Data.DataSetExtensions.dll and remove System.Collections.dll, so by this i can compile LINQ code as well as normal c# code also.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 22, 2013 3:56 AM

All replies

  • User-1910946339 posted

    I think your reference should be to

    System.Collections.Generic

    rather than

    System.Collections

    Wednesday, May 22, 2013 12:57 AM
  • User-541184106 posted

    Hi Paul,

            Thanks for reply, but i resolve my issue by adding reference to System.Data.DataSetExtensions.dll and remove System.Collections.dll, so by this i can compile LINQ code as well as normal c# code also.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 22, 2013 3:56 AM