Need help please

May 8 2006 12:21 PM
Hello,

I have a project where I need to compile and link some resources. This works great when the resources are compiled with al.exe, but it does not work when I compile it myself using a CSharpCodeProvider. The resources are written to a resource file using a ResourceWriter just prior to compilation of the .dll.

The resources manager wont read the generated .dll although when you compare the two files(the one complied with al.exe and the other) there aren't any differences.

Any suggestions? Thanks for your help

Code follows:

 

public static void MakeResourceDll(DataTable MDT)

{

string language = lang();

ResourceWriter rw = new ResourceWriter("SoftwareLanguage."+language + ".resources");

for (int i = 0; i < MDT.Rows.Count; i++)

{

string keys = MDT.Rows["Keys"].ToString();

string Values = MDT.Rows["Translations"].ToString();

rw.AddResource(keys, Values);

}

rw.Generate();

rw.Close();

 }

 //------------------------------------------------------------------------------------------

public static bool MakeCompiledDll(string language)

{

CSharpCodeProvider provider = new CSharpCodeProvider();

CodeCompileUnit ccu = new CodeCompileUnit();

ccu.ReferencedAssemblies.Clear();

CodeAttributeDeclaration att = new CodeAttributeDeclaration("System.Reflection.AssemblyVersion");

att.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression("1.0.0.0")));

ccu.AssemblyCustomAttributes.Add(att);

 

att = new CodeAttributeDeclaration("System.Reflection.AssemblyTitle");

att.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression("Company")Language Resource")));

ccu.AssemblyCustomAttributes.Add(att);

att = new CodeAttributeDeclaration("System.Reflection.AssemblyDescription");

att.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression("Translated to " + fulllang() + " by " + Author())));

ccu.AssemblyCustomAttributes.Add(att);

att = new CodeAttributeDeclaration("System.Reflection.AssemblyCompany");

att.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression("ACME")

ccu.AssemblyCustomAttributes.Add(att);

att = new CodeAttributeDeclaration("System.Reflection.AssemblyProduct");

att.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression("Company)));

ccu.AssemblyCustomAttributes.Add(att);

att = new CodeAttributeDeclaration("System.Reflection.AssemblyCopyright");

att.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(String.Format("Copyright (c) {1} {0}. All Rights Reserved.", DateTime.Now.Year, "Company"))));

ccu.AssemblyCustomAttributes.Add(att);

att = new CodeAttributeDeclaration("System.Reflection.AssemblyCulture");

att.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(language)));

ccu.AssemblyCustomAttributes.Add(att);

CompilerParameters cp = new CompilerParameters();

cp.GenerateExecutable = false;

cp.CompilerOptions = "/resource:TustenaLanguage." + language + ".resources";

cp.OutputAssembly = "SoftwareLanguage.resources.dll";

cp.GenerateInMemory = false;

ICodeCompiler myCodeCompiler = provider.CreateCompiler();

CompilerResults cr = myCodeCompiler.CompileAssemblyFromDom(cp, ccu);

if (cr.Errors.Count > 0)

{

string err = string.Empty;

Console.WriteLine("Errors building {0}", cr.PathToAssembly);

foreach (CompilerError ce in cr.Errors)

{

err+=string.Format(" {0}", ce.ToString());

}

MessageBox.Show(err);

}

else

{

MessageBox.Show(string.Format("{0} compiled successfully.", cr.PathToAssembly));

}

if (cr.Errors.Count > 0)

{

return false;

}

else

{

return true;

}

}