6

Using .NET 2.0.

System.Drawing is in my References list.

Here is my using statement:

using System.Drawing;

Here is the code:

private static Rectangle rScreen;

Here is the error on this line:

Error Text: The type or namespace name 'Rectangle' does not exist in the namespace 'System.Drawing' (are you missing an assembly reference?)

Why?

EDIT: Added compilation code:

Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("CompilerVersion", "v3.5");

CSharpCodeProvider codeProvider = new CSharpCodeProvider(dict);

CompilerParameters parameters = new CompilerParameters();

{
    parameters.ReferencedAssemblies.Add("System.Drawing.dll");

    parameters.ReferencedAssemblies.Add("System.dll");

    parameters.ReferencedAssemblies.Add("System.Core.dll");

    parameters.ReferencedAssemblies.Add("System.Data.dll");

    parameters.ReferencedAssemblies.Add("System.Data.Linq.dll");

    parameters.ReferencedAssemblies.Add("System.DirectoryServices.dll");

    parameters.ReferencedAssemblies.Add("System.Configuration.dll");

    parameters.ReferencedAssemblies.Add("System.Web.dll");

    parameters.ReferencedAssemblies.Add("System.Xml.dll");

    parameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");

    parameters.ReferencedAssemblies.Add("System.Web.Services.dll");

    parameters.ReferencedAssemblies.Add("System.ServiceModel.dll");

    parameters.ReferencedAssemblies.Add("System.IdentityModel.dll");

    parameters.ReferencedAssemblies.Add(string.Format(@"{0}{1}Microsoft.ReportViewer.Common.dll", AppDomain.CurrentDomain.RelativeSearchPath, @"\ReportViewer\"));

    parameters.ReferencedAssemblies.Add(string.Format(@"{0}{1}Microsoft.ReportViewer.WebForms.dll", AppDomain.CurrentDomain.RelativeSearchPath, @"\ReportViewer\"));
}

CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, sources.ToArray());

try
{
    ApexAssemblyManager.dynamicAssemblies.Add(hashKey, new DynamicAssemlby(results.CompiledAssembly));

    return ApexAssemblyManager.dynamicAssemblies[hashKey].CreateInstance(typeName);
}

All other ReferencedAssemblies work and have been working for a long time. This is the first time I have had such an error.

I have ensured and double checked that the reference is added. If I try to add it to the project again I get a message that the reference already exists.

Thanks

user390480
  • 1,655
  • 4
  • 28
  • 61
  • Is this with Visual Studio? Do you get a little red square on the bottom right of the word `Rectangle` in the editor with a suggested correction? Can you type `System.Drawing.Rectangle` instead and it will work? – John Alexiou May 16 '11 at 17:50
  • It compiles fine in Visual Studio. This is running as dynamically compiled in another application that has the using statement in it. – user390480 May 16 '11 at 17:57
  • a) try with the full scope of the type and b) is you declaration within scope of the `using` statement? c) Please edit the question with an explanation of what is dynamically compiled. – John Alexiou May 16 '11 at 18:04
  • Please post the compilation code. That is the code at fault in this scenario. Also, do you have .NET 4 installed on your development machine? Are you sure it's referencing the correct versions of the referenced assemblies? – Jeff Yates May 17 '11 at 13:52

1 Answers1

5

You state in the comments and tags that this is being dynamically compiled by another application. It is therefore likely that this other application is not including System.Drawing.dll as a reference when performing the compilation and therefore, the type is unresolved. It is not enough to merely state using System.Drawing, the assembly defining that namespace and its types must also be passed to the compiler.

In code, this is done using a CompilerParameters instance passed via one of the CompileAssemblyFrom... calls to the CodeDomProvider instance that is performing the compilation (in this case, the CSharpCodeProvider). The CompilerParameters.ReferencedAssemblies collection indicates to the compiler what assemblies to look at when trying to perform type resolution.

Update
Try explicitly adding mscorlib to the references.

Also, I don't know if this is related, but as you mentioned a server (is it a service of some kind?), MSDN states:

Classes within the System.Drawing namespace are not supported for use within a Windows or ASP.NET service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions. For a supported alternative, see Windows Imaging Components.

I'd say this qualifies as an unexpected problem although I wouldn't expect problems to manifest during a compile process, but rather when executing code. That said, they don't really specify that in the documentation, so it could apply to the use of System.Drawing.dll as a reference in general.

Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
  • Yes, I have it: parameters.ReferencedAssemblies.Add("System.Drawing.dll"); I even tried changing the order relative to the other ReferencedAssemblies. Yeah, running out of ideas. – user390480 May 17 '11 at 13:29
  • @user390480: But it isn't finding it. Are you sure that type resolution is locating it properly? Do you know what paths it is searching when looking for assemblies? Post your compilation code - perhaps there is an error in there. – Jeff Yates May 17 '11 at 13:41
  • On my local machine the version of System.Drawing.dll is 2.0.50727.5420 while on the server the version is 2.0.50727.3053. Would that make a difference? – user390480 May 17 '11 at 14:34
  • @user390480: What worked? Was it `mscorlib` or the "unexpected problems" thing? I'm intrigued. – Jeff Yates May 17 '11 at 19:27