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
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.dllas a reference when performing the compilation and therefore, the type is unresolved. It is not enough to merely stateusing System.Drawing, the assembly defining that namespace and its types must also be passed to the compiler.In code, this is done using a
CompilerParametersinstance passed via one of theCompileAssemblyFrom...calls to theCodeDomProviderinstance that is performing the compilation (in this case, theCSharpCodeProvider). TheCompilerParameters.ReferencedAssembliescollection indicates to the compiler what assemblies to look at when trying to perform type resolution.Update
Try explicitly adding
mscorlibto 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:
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.dllas a reference in general.