0

My issue goes like this:

There is a project called myframework. It has some extension methods defined in it as follows:

namespace myframework
{
    public static class Helpers
    {
        public static bool ContainsAll(this string obj, string[])
        {
            return true;
        }
    }
}

It also has some other stuff like interfaces, etc, etc.

There is a second class I generate via System.CodeDom classes. The generated output is somewhat like this:

using myframework;


public class A: IMyFrameworkInterface

{
    public void foo()
    {
        string s ="HELLO";

        if(s.ContainsAll(some_arr))
            return;
    }
        //More methods defined...

}

The compiler options I pass which is created prior to the actual compile call references the correct assemblies

var cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("System.dll");
cp.ReferencedAssemblies.Add("myframework.dll");

The code compilation modules are written in a different project. The particular class responsible for this also nicely gives us access to a list of CompilerError object via which we can learn the result of compilation.

Issue1: When I tried this in an asp.net project the compiler threw error saying it could not find metadata file myframework.dll (despite it being referenced in the project).

Issue2: When I tried it with a windows forms project. It gave a different error. This time saying that string does not contain definition for ContainsAll()

How to solve these two specific problems?

svick
  • 236,525
  • 50
  • 385
  • 514
deostroll
  • 11,661
  • 21
  • 90
  • 161

1 Answers1

1

Found out the answer to this after a bit digging up. I was using .net framework 3.5. The codedom compiler apis targets v2.0 of the framework by default. Hence, you have to manually specify the correct framework:

var cp = new CompilerParameters(
    new Dictionary<string,string>() { {"CompilerVersion", "v3.5"} });

For the compilation to work within an asp.net environment you'd have to actually point the references to the correct location. Hence you'd have to do something like follows:

cp.ReferencedAssemblies.Add(
    HttpContext.Current.Server.MapPath(
        "bin\\myframework.dll"));

My references:

  1. http://blogs.msdn.com/b/lukeh/archive/2007/07/11/c-3-0-and-codedom.aspx
  2. .Net 3.5 CodeDom Compiler generating odd errors
  3. And comments in the question's post. :)
Community
  • 1
  • 1
deostroll
  • 11,661
  • 21
  • 90
  • 161