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.


Lance Johnson

Re: Types vs. Attributes by Daniel

Daniel
Fri Sep 10 08:16:30 CDT 2004

I believe attribute discovery uses reflection, which is going to introduce a
performance hit, but I don't think there's going to be a great performing
way to do this. I mean you're scanning the directory for assemblies, then
opening each of those at looking at each class? I wouldn't expect that to
be speedy.

But in regards to attributes, why can't you just check using "is" or
"IsDerivedFrom"? Even if there's no performance difference you seem to have
introduced an attribute to accomplish something you don't need to.

"Lance Johnson" <ljohnson@docs.com> wrote in message
news:%23myo7QblEHA.2680@TK2MSFTNGP15.phx.gbl...
> 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.
>
>
> Lance Johnson
>
>



Re: Types vs. Attributes by Richard

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/