Richard
Fri Sep 17 11:06:41 CDT 2004
Lance Johnson wrote:
> We want to be able to load the classes from a directory that derive
> from a particular base class. So the question is whether attributes
> for each deriving class will help us in finding these classes faster
> or not?
>
> We currently have attributes and use those, but it seems very slow to
> go through all the assemblies and try to get all of the attributes
> from there. I'm looking for a way to speed this up and wanted to know
> if anybody is doing something similar and had any pointers as far as
> speed.
When you test for an attribute you:
1) use Reflection
2) create an instance of the attribute class initialized with the data in
the metadata
You cannot get away from the reflection call, but the instantiation of the
attribute object is likely to take time. A quicker mechanism is to use an
empty interface:
interface IMyInterface{/* empty */}
// can search for this one
public class ClassA : IMyInterface{}
// cannot search for this one
public class ClassB{}
// Search with this code
void DumpTypes(Assembly a)
{
Type[] types = a.GetTypes();
foreach(Type type in types)
{
if (type.GetInterface("IMyInterface") != null)
Console.WriteLine("found " + type.ToString());
}
}
Richard
--
.NET training, development, consulting and mentoring
my email evpuneqt@zicf.bet is encrypted with ROT13 (www.rot13.org)
sign up for my free .NET newsletter at
http://www.wd-mag.com/newsletters/