Types vs. Attributes

  • Thread starter Thread starter Lance Johnson
  • Start date Start date
L

Lance Johnson

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
 
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 said:
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
 
Hello Richard Grimes [MVP],

Would it make more sense to do something like this?

void DumpTypes(Assembly a)
{
foreach (Type type in a.GetTypes()) // this could also be a.GetExportedTypes so that only publicly scoped types are returned
{
if (type.IsSubclassOf(typeof(MyBaseClass))
{
Console.WriteLine("found " + type.ToString());
}
}
}

This gets around using a placeholder interface for this...

--
Matt Berther
http://www.mattberther.com
Lance said:
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
 
Back
Top