CustomAttributeBuilder and Regex.CompileToAssembly()

G

Grant Johnson

Hi,

I am trying to create an assembly to store compiled regular expressions.
I need to give this assembly a strong name. However, I can't seem to
get my use of the CustomAttributeBuilder class right.

Here's what I'm doing:


// The regular expression
infos = new RegexCompilationInfo[1];
infos[0] = new RegexCompilationInfo(@"[\x80-\x9f]",
RegexOptions.Compiled | RegexOptions.CultureInvariant |
RegexOptions.IgnoreCase, "C1Regex", IdsNamespace, true);


// Build the assembly
AssemblyName an = new AssemblyName();
an.Name = IdsNamespace;

Type t;
object[] args;
ConstructorInfo ci;
CustomAttributeBuilder[] ab = new CustomAttributeBuilder[1];

t = typeof(AssemblyKeyFileAttribute);
ci = t.GetConstructor(new Type[] { typeof(string) });
args = new object[1] { @"C:\MyKeys.keys" };
CustomAttributeBuilder cab = new CustomAttributeBuilder(ci, args);

ab[0] = cab;

Regex.CompileToAssembly(infos, an, ab);


This code compiles and runs, but sn.exe reports the produced assembly
does _not_ have a strong name. Can anyone see what I'm doing wrong?

Thanks in advance,

Grant
 
N

Nicholas Paldino [.NET/C# MVP]

Grant,

The AssemblyKeyFileAttribute (and the other attributes which indicate
how to strong name an assembly) are actually faux-pauxs (sp) on MS's part.
The reason this is so is because the assembly now contains information about
where the private key is located (which is valid in some context which might
or might not be known) in the assembly itself. This is a security violation
which is fixed (although the attributes are still supported) by allowing the
key information to be passed to the compiler, and not stored in the
assembly.

However, that doesn't really answer your question, but it gives some
background. The attribute in code is really nothing more than a marker.
However, having the attribute doesn't mean that the assembly is
strong-named. Rather, it tells the compiler to strong-name the assembly
when the code is compiled. What makes the assembly strong named is a
public/private key combo which is used to digitally sign the assembly.

To get this to happen when your assembly is compiled, you should set the
KeyPair property of the AssemblyName to the public/private key combination
used to sign the assembly. Additionally, make sure you set the version of
the assembly as well through the Version property. I'm guessing this will
result in the resulting assembly being strong-named.

Hope this helps.
 
G

Grant Johnson

Hi Nicholas,

Thank you for your detailed response! I'll try this today and let you
know how I get on.

Regards,

Grant
 
G

Grant Johnson

Hi Nicholas,

Works like a charm! We didn't need to supply the version attribute, but
we will anyway.

Again, thanks for your help.

Regards,

Grant
 

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