CodeDomProvider.CreateCompiler() is obsolete fix

A

Andrus

In code below line

ICodeCompiler loCompiler = new CSharpCodeProvider().CreateCompiler();

in .net 2 causes warning

'System.CodeDom.Compiler.CodeDomProvider.CreateCompiler()' is obsolete:
'Callers should not use the ICodeCompiler interface and should instead use
the methods directly on the CodeDomProvider class. Those inheriting from
CodeDomProvider must still implement this interface, and should exclude this
warning or also obsolete this method.'

How to rewrite code so that warning does not appear ?

code is:

ICodeCompiler loCompiler = new CSharpCodeProvider().CreateCompiler();
CompilerParameters loParameters = new CompilerParameters();
loParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
loParameters.GenerateInMemory = false;
loParameters.OutputAssembly = "myasm.dll";


CompilerResults loCompiled =
loCompiler.CompileAssemblyFromSource(loParameters, lcCode);



Andrus.
 
?

=?iso-8859-2?Q?=A3ukasz_'Maly'_Ostrowski?=

Andrus said:
How to rewrite code so that warning does not appear ?

code is:

ICodeCompiler loCompiler = new CSharpCodeProvider().CreateCompiler();
CompilerParameters loParameters = new CompilerParameters();
loParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
loParameters.GenerateInMemory = false;
loParameters.OutputAssembly = "myasm.dll";


CompilerResults loCompiled =
loCompiler.CompileAssemblyFromSource(loParameters, lcCode);


Use the following:

CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("System.Windows.Forms.dll")
cp.GenerateInMemory = false;
cp.OutputAssembly = "myasm.dll";

CompilerResults result =
provider.CompileAssemblyFromSource(cp, sourceCode);

Orcas beta2 doesnt throw any deprecation warnings
for this snippet ;).
 
A

Andrus

Use the following:
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("System.Windows.Forms.dll")
cp.GenerateInMemory = false;
cp.OutputAssembly = "myasm.dll";

CompilerResults result =
provider.CompileAssemblyFromSource(cp, sourceCode);

Orcas beta2 doesnt throw any deprecation warnings
for this snippet ;).

£ukasz,

thank you.

How to create signed assembly using provider ?
I think I must pass snk file as parameter but how ?

Andrus.
 
?

=?iso-8859-2?Q?=A3ukasz_'Maly'_Ostrowski?=

Andrus said:
How to create signed assembly using provider ?
I think I must pass snk file as parameter but how ?


First, use the sn.exe tool to generate a keyfile:

$ sn -k someKeyFile.snk

Then, in Your code, add the CompilerOptions setting:

CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("System.Windows.Forms.dll")
cp.GenerateInMemory = false;
cp.OutputAssembly = "myasm.dll";

cp.CompilerOptions = "/keyfile:someKeyFile.snk";

And then upon compilation, the compiler will embeed
the strong name key file to the assembly therefore
making it (strongly named/)(gac installable).
 
A

Andrus

First, use the sn.exe tool to generate a keyfile:
$ sn -k someKeyFile.snk

Then, in Your code, add the CompilerOptions setting:

CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("System.Windows.Forms.dll")
cp.GenerateInMemory = false;
cp.OutputAssembly = "myasm.dll";

cp.CompilerOptions = "/keyfile:someKeyFile.snk";

And then upon compilation, the compiler will embeed
the strong name key file to the assembly therefore
making it (strongly named/)(gac installable).

£ukasz,

thank you.
I create assemblies at runtime.
I think that cp.CompilerOptions = "/keyfile:someKeyFile.snk";
requires to distribute private key as separate file with my application.

How to embed snk file into application as resource and force compiler to use
it from resource
or generate it at runtime ?

Andrus.
 
?

=?iso-8859-2?Q?=A3ukasz_'Maly'_Ostrowski?=

Andrus said:
I create assemblies at runtime.
I think that cp.CompilerOptions = "/keyfile:someKeyFile.snk";
requires to distribute private key as separate file with my
application.
Indeed.

How to embed snk file into application as resource and force compiler
to use it from resource
or generate it at runtime ?


Add a new Resource File to the Project, Add the .snk key file to it,
if the Resource File name was "Resource", and the file name was
keyFile.snk, You will get some resource identifier like
Resource.keyFile typed as byte[].

Create a file stream, and write the byte array to it, You'll get
the resource-embeeded key file as a normal disk file, therefore
You'll be able to use it during assembly compilation.

using (FileStream fs = File.Create("keyFile.snk"))
fs.Write(Resource.keyFile, 0, Resource.keyFile.Length);

And then

cp.CompilerOptions = "/keyfile:keyFile.snk"

should work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top