Assembly.LoadFile and Code Analysis warning CA2001


J

Jesse Houwing

Hey all,

I've implemented a simple plug-in based file parser for a customer and
I'm running into CA2001, which I'd like to solve.

The problem is that I cannot find any information on how to solve this
error, other than removing my call to Assembly.LoadFile. There are some
alternatives, but they are either also in the blacklist for CA2001, or
they're deprecated.

What I'm trying to accomplish is to load my file format description
(classes decorated with custom attributes) from a specific directory
(e.g. /bin/formats).

I'm using the following code:

foreach (string dll in
Directory.GetFiles(Path.Combine(Directory.GetCurrentDirectory(),
"Formats"), "*.dll"))
{
Assembly ass = Assembly.LoadFile(dll);
Type[] types = ass.GetTypes();
}

Any other approaches I could use for this? Can I tell the framework to
load all my assemblies by adding a config directive? Or is there a Load*
method in Assembly that I might have missed?
 
Ad

Advertisements

J

Jeroen Mostert

Jesse said:
I've implemented a simple plug-in based file parser for a customer and
I'm running into CA2001, which I'd like to solve.
It would have behooved you to spell out what CA2001 was. Would've saved me a
trip to Google.

For the rest of us, it's "Avoid calling problematic methods", blacklisting
several methods with problems, including Assembly.LoadFile().
The problem is that I cannot find any information on how to solve this
error, other than removing my call to Assembly.LoadFile.

Basically, it's because Microsoft wants you to use the GAC, or else muck
around with a new AppDomain. See
http://blogs.msdn.com/suzcook/archive/2003/05/29/57143.aspx and
http://blogs.msdn.com/suzcook/archive/2003/06/13/57180.aspx.

Personally, I'd just suppress the CA2001 warning. This is one area where
Microsoft's ideas of the perfect world just don't mesh with what people
actually want.
 

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