Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Rectangle' does not exist in the namespace 'System.Drawing'

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

like image 561
user390480 Avatar asked May 16 '11 17:05

user390480


1 Answers

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.

like image 163
Jeff Yates Avatar answered Sep 22 '22 16:09

Jeff Yates